user6029972
user6029972

Reputation:

Error with Instantiate?

I'm getting error on second line with this code:

var myNewSmoke = Instantiate (cube, temp, transform.rotation);
myNewSmoke.transform.parent = gameObject.transform;

error:

Type UnityEngine.Object does not contain a definition for transform and no extension method transform of type UnityEngine.Object could be found (are you missing a using directive or an assembly reference?)

I don't understand the error. What's the problem?

Upvotes: 0

Views: 66

Answers (1)

Aizen
Aizen

Reputation: 1825

Instantiate is a method of Object Class. You're trying to access gameobject.transform which is inside GameObject Class.

GameObject Class Implements Object Class like everything else so you have to cast it into GameObject first.

var myNewSmoke = Instantiate (cube, temp, transform.rotation) as GameObject;

Or

GameObject myNewSmoke = Instantiate (cube, temp, transform.rotation) as GameObject;

Upvotes: 1

Related Questions