swiftsknode

Value of type 'SKNode' has no member 'texture'. But it does


I created this SpriteKitNode:

let examp = examps[examps.count-1] // examps is a global array with SKNodes inside of it 

But when I try to change its texture I receive this error:

examp.texture = SKTexture(imageNamed: "examp2")

Value of type 'SKNode' has no member 'texture'

However, it has a value of 'texture' so I don't know what the solution is.

https://imgur.com/a/s1V0XZF


Solution

  • examps is [SKNode]. This means that examp is SKNode, not SKSpriteNode even if examps[examps.count-1] happens to hold an SKSpriteNode.

    In order to access the texture property, you need to cast the result of examps[examps.count-1] to an SKSpriteNode.

    if let examp = examps[examps.count-1] as? SKSpriteNode {
        examp.texture = SKTexture(imageNamed: "examp2")
    }