- Home /
Instantiate clones with error.
When i'm instantiating some clouds to my game, the clones dont get the game object, being "none" it appears a lot of errors in my console but the game doesnt stop.
look the picture
I should do something in my scripts to those clones get the game Object, called "Nuvem", like in the Hierarchy you can see.
i'll post my scripts here.
First the cloud script
public class nuvemController : MonoBehaviour {
public float velocidade;
public Transform transformNuvem;
public GameObject objectNuvem;
void Start () {
}
// Update is called once per frame
void Update () {
transformNuvem.Translate(velocidade * Time.deltaTime* -1, 0, 0);
if (objectNuvem.transform.localPosition.x < -13){
Destroy(objectNuvem);
}
}
}
now the instantiator script
public class Instanciador : MonoBehaviour {
public GameObject prefab;
public float rateSpawn;
public float currentTime;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
InstanciarNuvem();
}
void InstanciarNuvem()
{
currentTime += Time.deltaTime;
if (currentTime >= rateSpawn)
{
currentTime = 0;
Instantiate(prefab, transform.position, transform.rotation);
}
}
}
I think that i should put something in the Start() to initializate those clones with the cloud object. how should i correct this error?
How are you assigning the Transform and GameObject to the nuvemController script?
$$anonymous$$y prefab "Nuvem" is receiving the gameObject and the Transform from the NuvemController, but when the prefab creates its clones, they dont have the gameObject, causing the error.
sorry for my bad explaining, i'm not still good at English '-'
Answer by KidRigger · Apr 20, 2016 at 05:12 PM
As you want the objects own Transform and GameObject, get the components in Start function, using GetComponent() function.
edit: I think you should not keep the prefab in the hierarchy. Once it gets destroyed the Instanciador script will not be able to reference. Rather just keep nuvem in the Prefabs folder and let the Instanciador spawn its clones only.
Heey Really THAN$$anonymous$$S ! That was the problem, my prefab could not be in scene. Now it's working perfectly. I just took off the prefab from the scene and drag the own prefab to the GameObject field.
That problem bugged me a lot. :) It basically uses the instance, so when it is destroyed, you lose the reference. The Prefab reference that you now have will stay till the end. Nice to help you. ($$anonymous$$y first answer XD)