- Home /
How do you choose where your object Instatntiates?
hey guys do you know how to choose where your object instantiates. like can you choose an object for the cloned object to appear? here is my script. It is attached to what i want to clone. Oh and you don't have to but, do you know how to get that GUI text to work? on my script? I would love it if you did. Thanks guys for your help.
public class findweaponrocket : MonoBehaviour {
public string text = "You got the rocket launcher!, press F to pick it up!";
public Transform rocketlauncher;
public Transform rocketlauncherpoint;
// Use this for initialization void Start () {
}
// Update is called once per frame
void OnCollisionEnter(Collision collision) {
if (collision.collider.tag == "Bullet")
{ GameObject clone;
clone = Instantiate(gameObject,transform.position, transform.rotation) as GameObject;
Debug.Log ("yay");
text = GUI.TextArea(new Rect(10, 10, 200, 100), text, 2000); } } }
Answer by sam32x · Aug 14, 2012 at 09:21 AM
change transform.position,transform.rotation in clone = Instantiate to rocketlauncherpoint.position,rocketlauncherpoint.rotation (if thats the point where you want it)
Answer by Kryptos · Aug 14, 2012 at 09:39 AM
Lets take a look at the Instantiate method:
static Object Instantiate (Object original, Vector3 position, Quaternion rotation)
First argument is the object to be cloned. It can be either a prefab or an object in the scene.
Second argument is a
Vector3
. It is the position where the cloned object will be placed by default.Third and last argument is a
Quaternion
. It is the rotation of the cloned object.
In fact, the following lines of code are equivalent:
GameObject clone = Instantiate(prefab, v3Position, qRotation) as GameObject;
// or
GameObject clone = Instantiate(prefab) as GameObject;
clone.transform.position = v3Position;
clone.transform.rotation = qRotation;
As for the GUI text, you need to set it in the OnGUI method. See GUI Scripting Guide.