Manipulating Instanced Object

Hi, my goal is to Instantiate an object at a start of a game and Set its parent to a pawn. The function that creates Instances looks like so :

	void CreateRocket_L()
{
		Rocket_L = Instantiate(rockets, 
					new Vector3(TopDown_Controller.Instance.transform.position.x + 0.85f,
	                        	TopDown_Controller.Instance.transform.position.y,
	                        	TopDown_Controller.Instance.transform.position.z), 
					Quaternion.identity) as rocket;		

	Debug.Log(Rocket_L);
		//Rocket_L.transform.parent = TopDown_Controller.Instance.transform;
}

The problem is that Debug.Log shows that Rocket_L is Null… If I try to do anything to Rocket_L Unity says there is no reference to an object in Rocket_L (Debug.Log agrees). Setting parent after Debug.Log is commented out since Unity says Rocket_L is null but I left it there so you could see whats the point. I found in Unity Script Reference:

    public class example : MonoBehaviour {
public Rigidbody projectile;
void Update() {
if (Input.GetButtonDown("Fire1")) {
Rigidbody clone;
clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
clone.velocity = transform.TransformDirection(Vector3.forward * 10);
}
}
}

The principle is the same here but they set the velocity of a clone object with no problem. Any idea why my isn’t working ?

I suspect your problem is a confusion about types. Instantiate returns the same type of the prefab, thus you should have a rocket type defined elsewhere, and the variables rockets and Rocket_L should be of type rocket - a bad idea, unless you know exactly what you’re doing with this custom type rocket.

You can simplify things: declare rockets and Rocket_L as Transform, like below:

Transform rockets; // rocket prefab
Transform Rocket_L; // instantiated rocket

void CreateRocket_L()
{
    Vector3 pos = TopDown_Controller.Instance.transform.position;
    pos.x += 0.85f; // <- using a different way to specify position
    Rocket_L = Instantiate(rockets, pos, Quaternion.identity) as Transform;       
    Debug.Log(Rocket_L);
    Rocket_L.parent = TopDown_Controller.Instance.transform;
}

NOTE: I used the pos auxiliary variable just to simplify the writing - this has nothing to do with the problem you had.

Ohhh sorry I didn’t mention but i do have class rocket. But I will try doing it with Transform.