- Home /
Prefab clone not working
Hi everyone,
Im very new to unity and I am trying to create 5 of the same game object using a prefab but for some reason it does not work. (http://docs.unity3d.com/Manual/InstantiatingPrefabs.html iv been using this)
I have done all of the instructions (create cube, add rigidbody and drag it into the prefab) and the code in the script is as followed
using UnityEngine;
using System.Collections;
public class multiboxes : MonoBehaviour {
public Rigidbody boxR;
// Use this for initialization
void Start ()
{
for (int i = 0; i < 5; i++)
{
Vector3 position = new Vector3 (Random.Range (-3.0f, 3.0f), Random.Range (-2.0f, 2.0f), -2.0f);
Rigidbody boxclone = (Rigidbody)Instantiate (boxR, position, Quaternion.identity);
}
}
// Update is called once per frame
void Update ()
{
}
}
I have also made sure that I dragged the prefab into the BoxR rigidbody.
Any help will be very appreciated.
the problem I am having is that the cubes are not appearing on the screen
the camera is at 0,0-10 (is faceing the correct direction) and as you can see from the screen shot, gravity is off and there is a Rigidbody (it was on the component when I dragged it over) you have any other ideas this is really wierd?
I think the reason they are not being created is because you are instantiating RigidBodies. You should be instantiating GameObjects. I'll get back with you in a few $$anonymous$$utes with working code.
Answer by JSierraAKAMC · Nov 03, 2014 at 11:01 PM
Here, try this code: using UnityEngine; using System.Collections;
public class multiboxes : MonoBehaviour {
public GameObject boxR;
// Use this for initialization
void Start ()
{
for (int i = 0; i < 5; i++)
{
Vector3 position = new Vector3 (Random.Range (-3.0f, 3.0f), Random.Range (-2.0f, 2.0f), -2.0f);
GameObject boxclone = (GameObject)Instantiate (boxR, position, Quaternion.identity);
}
}
// Update is called once per frame
void Update ()
{
}
}
The reason it wasn't working is that you were trying to instantiate Rigidbodies, which are just components. To actually create an object, you would have to instantiate a GameObject. I hope this helps.
Oh, and make sure to attach this script to the camera, not the cube itself.
can I just ask if you dont $$anonymous$$d why it seemed to work for robertbu and not me ?