- Home /
Script Initializing help???!!!
Is there a way to only run the script when the game object is spawned within the game itself.
For example. i have these two scripts, the Start and the Spawn.
I want everything within the Start script to run only when the Spawn class spawns out the enemy.
Sorry for again. Er.
in my Starts Class script i have this function
function Start() {
getStart = GameObject.FindWithTag("Start");
getArray = getStart.GetComponent(StageWaypoint).carrotArray;
}
in my spawn class, i have the
function Update() {
for (var x = 0; x < carrotNo ; x ++) { var pos = Vector3( ((x + randPos) ) , 0.3, 0) * spacing ;
//Debug.Log(spacing + " spacing");
if(x < 5)
{
CarrotIn1 = Instantiate(carrotPrefab1, pos, Quaternion.identity);
CarrotIn1.renderer.material.mainTexture = texture1;
carrotArray.Push(CarrotIn1);
}
else if( x > 4)
{
pos = Vector3 ((x + (randPos + randDiff) ), 0.3, 0)* spacing;
CarrotIn2 = Instantiate(carrotPrefab2, pos, Quaternion.identity);
CarrotIn2.renderer.material.mainTexture = texture2;
carrotArray.Push(CarrotIn1);
}
}
}
however my method is that when i run the Start function in the Starts script, getArray = nothing as the carrot have not been spawned yet. Therefore, is there a way whereby, i could only run the
getArray = getStart.GetComponent(StageWaypoint).carrotArray;
after that carrot have been spawned and placed into the array
thanks alot. =D
Sorry for asking this dumb question. =/
What is the name of your start script, and where is it running (on the same gameobject?)
Start script is attached to a empty gameobject where else spawn is attached to my player.
Does the start script do anything else than that? Couldn't you just add a new instance of it once you add a new carrot? And do you really spawn multiple carrots per frame update?
nope the Start script dont do anything except linking all the different components from different script together.
anyway i tried another approach whereby i placed that line of script within the update function and turning it off once it run that line of code.
Answer by Atnas1010 · Nov 04, 2010 at 03:12 AM
Are you putting your code in function Start()?
You need to explain yourself better and/or post some of your code
Answer by Avery · Nov 04, 2010 at 04:17 AM
function Update() { if(noo1 == 0) { for (var i : int = 0 ; i < getArray.length; i++) { thisNums += 1;
if(thisNums < 20)
{
getArray = getStart.GetComponent(StageWaypoint).carrotArray;
noo1 = 1;
}
Debug.Log(getArray[i] + " " + thisNums);
}
}
}
anyways this the way i does it for now. I not sure, whether this method is a good way though as i aint experience with scripting. =/ is there anyway better to do this?