pragma transform.parent

Hi, i am new to strict pragma in javascript. I get this error: "transform is not a member of UnityEngine.Object"

This is the code I'm using:

var Sparks : ParticleEmitter;
var SparksInstance : GameObject = Instantiate (Sparks, BombPos.position, Bomb.rotation);
SparksInstance.transform.parent = Bomb;

Anyone has a suggestion?

2 Answers

2

I think it's because it returns a reference to the objects particle emitter, not a reference to the game object.

var SparksInstance : ParticleEmitter = Instantiate (Sparks, BombPos.position, Bomb.rotation); 
SparksInstance.transform.parent = Bomb;

Did you instantiate "SparksInstance" via script? If you look at the Instantiate documentation, you'll see it returns a UnityEngine.Object. But don't worry! If the object you instantiated is a GameObject, what you'll get back is also a GameObject, you just have to explicitly "cast" it as one:

//C#
GameObject sparksInstance = (GameObject)Instantiate (sparksPrefab);
// The "(GameObject)" in front of the Instantiate call treats the returned object as a GameObject.

//Javascript
var sparksInstance : GameObject = Instantiate (sparksPrefab)
// In Javascript/Unityscript, if you declare the type of the variable with its declaration, the compiler figures out what you mean to do.

ETA: Based on the code you added to your original question, you're making two steps at once. "Sparks" is a ParticleEmitter, not a GameObject. Now, it's attached to a GameObject, but you can't just treat it as one. Try this:

var SparksInstance : GameObject = (Instantiate (Sparks, Bomb.position, Bomb.rotation) as ParticleEmitter).gameObject;

If you try to copy/paste this code, note that I've used the more typical first character lowercase for variable names. Adjust accordingly.

See updated answer.