- Home /
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 ?
Answer by aldonaletto · Dec 08, 2011 at 11:06 AM
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.
Instantiate(obj,...) clones the object obj; if obj is a component like Rigidbody, Transform, Renderer etc. the whole GameObject to which the component belongs is cloned, and the reference returned is the same component passed to Instantiate. In the docs example, a Rigidboy was passed to Instantiate: the whole GameObject was cloned, and the reference returned was its Rigidbody.
In your specific case, you passed a rocket reference, which isn't part of UnityEngine - I suspect that Unity may even have cloned the object, but having found no rocket component, returned null.
Passing its Transform, the GameObject will be cloned and its transform returned, but probably you will have to instantiate yourself the rocket class, and assign the new object to it.
Answer by khatake · Dec 08, 2011 at 12:45 PM
Ohhh sorry I didn't mention but i do have class rocket. But I will try doing it with Transform.
@khatake, please click the more button and convert this answer to a comment in my answer (the Your box must be used only to answer the question; comments and replies must be posted with add new comment).
Your answer
Follow this Question
Related Questions
Random array issue C# 2 Answers
Instantiating as a child error 1 Answer
Instantiated GameObject gets spawned as a child 2 Answers
overlapping object doesn't disable 1 Answer
How do I instantiate certain objects to appear in a specific spot? 3 Answers