- Home /
Simple clone scripting
I'm a complete noob just pootling along and trying to make a fish-tank with an ecosystem for fun. I want to start the base of the food chain with plankton but I'm having difficulty, as soon as I try to add a variable to the instantiated prefab I get a null object reference.
There are two scripts, the first is used to generate the plankton;
var Plankton_Archae_Prefab:GameObject;
function Start () {
for (var i : int = 0;i < 100; i++) {
var position = Vector3(Random.Range(0, 500),Random.Range(100, 300), Random.Range(0, 500));
Instantiate(Plankton_Archae_Prefab, position, Quaternion.identity);
}
}
The second script is attached to an individual plankton, this is the one that throws a wobbly;
var Plankton_Archae_Prefab:GameObject;
var Currentage; //Current Age of Plankton var Maxage; //Maximum Age of Plankton var hp; //Hit Points var div; //Number of cell divisions
var speed = 5; //Movement speed
var PlankEnergy; //Energy of plankton
function Update () { PlankEnergy++; rigidbody.position = Vector3.Lerp(rigidbody.position,rigidbody.position + Vector3((Random.value-0.5)*speed,(Random.value-0.5)*speed,(Random.value-0.5)*speed),Time.time); if(PlankEnergy==10){ Instantiate(Plankton_Archae_Prefab, rigidbody.position, Quaternion.identity); } }
I've tried moving the PlankEnergy variable into the update function, making it a private variable and all sorts. If I remove the PlankEnergy variable then the script works fine, problem is I need that variable :-)
Note: Although it's designed for plankton the basic script is really a study in bacteria.
This may sound silly, but have you assigned the value of Plankton_Archae_Prefab on the script attached to the plankton?
Answer by I B Delta 1 · Oct 07, 2010 at 10:47 AM
I think this is what you mean;
var Plankton_Archae_Prefab:GameObject;
var Currentage; //Current Age of Plankton var Maxage; //Maximum Age of Plankton var hp; //Hit Points var div; //Number of cell divisions
var speed = 5; //Movement speed
var PlankEnergy; //Energy of plankton
function Start (){ //Just added PlankEnergy==0; // Sets starting value }
function Update () { PlankEnergy++; rigidbody.position = Vector3.Lerp(rigidbody.position,rigidbody.position + Vector3((Random.value-0.5)*speed,(Random.value-0.5)*speed,(Random.value-0.5)*speed),Time.time); if(PlankEnergy==10){ Instantiate(Plankton_Archae_Prefab, rigidbody.position, Quaternion.identity); } }
It still failed to work :-( All of the links in the inspector have been triple checked and all are in place. It appears to be trying to access the wrong script when it runs.
Your answer
![](https://koobas.hobune.stream/wayback/20220613075010im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Instatiating a prefab in a random position 0 Answers
Moving with instantiated projectile 2 Answers
Instantiate script then edit its variables 2 Answers
Script that switches between first and third person controller 3 Answers
Clones with individual scripts 1 Answer