0

I am trying to access / modify the properties of an SCNSphere that I have in a SCNScene. I have the scene pre-set as a file called "spaceScene.scn". I am loading the scene as such

self.sceneView.scene = [SCNScene sceneNamed:@"spaceScene.scn"];
self.sceneView.allowsCameraControl = YES;
self.sceneView.scene.rootNode.camera = [SCNCamera camera];
SCNSphere *earth = (SCNSphere *)[self.sceneView.scene.rootNode childNodeWithName:@"earth" recursively:NO];

NSMutableArray *materials = earth.materials;
NSLog(@"Materials of earth from scene: %@", materials);

I can not seem to get past reading the materials property of the SCNSphere earth. I keep getting an instance error:

-[SCNNode materials]: unrecognized selector sent to instance 0x1701c5550

Feeling a little silly with this issue but please someone just tell me how to access the sphere properties? thanks

Will Von Ullrich
  • 2,129
  • 2
  • 15
  • 42

2 Answers2

2

You are casting SCNNode to SCNSphere when you are creating your earth object.

If you take a look at the documentation, the function you are using is returning SCNNode.

- (SCNNode *)childNodeWithName:(NSString *)name 
                   recursively:(BOOL)recursively;

With the casting, you can fake that the object is SCNSphere, however, it is not. And when you are sending the materials message to the object, it crashes, because it is an unrecognized selector on SCNNode.

I would recommend not to force cast, and look for an other way to retrieve you object.

dirtydanee
  • 6,081
  • 2
  • 27
  • 43
  • I understand that unfortunately right after having posted this. Clearly it knows it is NOT a sphere given it's telling me SCNNode materials is the issue. Not sure how else to retrieve the object. Ultimately I am trying to set an SKScene as the surface so I can add and interact with objects on the face of the sphere – Will Von Ullrich Nov 02 '16 at 18:58
2

SCNSphere does not inherit from SCNNode. You should rather retrieve the node's geometry which can be a sphere.

mnuages
  • 13,049
  • 2
  • 23
  • 40