I have this code that has a spawner… each spawner can spawn its own bad guys. When I spawn a bad guy I basically say this:
var gol = Instantiate(tokenType1, new Vector3(this.transform.position.x, this.transform.position.y, this.transform.position.z-1.5), Quaternion.Euler(270,0,0)) as GameObject;
gol.AddComponent("badGuy");
This works but in the future I want to be able to change data on the fly for the game object above.
For instance using the above example i want this:
Spawner spawns cylinder
Spawn says add Componeent “badGuy” to cylinder (later replaced by a model).
Then somehow I can set the variable of the area the bad guy is in via the var gol reference above:
gol.setArea(leftArea);
Is this valid so I can set the spawned objects internal variable right upon it being created??
I am not attaching this script to the bad guys in the inspector because they are instantiated dynamically.
I also have another question:
The above spawners are all parts of a group like perhaps there are 4 spawners to the left…only one should spawn at a time not all of them. I need some over all game object that knows about all these spawners and can tell which one in particular to spawn. I have no clue what approach to use. As each of these spawners themselves this group of four to the left are created at runtime as well…
So what approach would I use to have the over all game object like “spawnTImeChecker” check if its time to spawn and pick one from that group of many at random and tell it to spawn (invoking the code further above) spawning a bad guy.
I tried this but it gives me an error ‘No appropriate version of ‘UnityEngine.GameObject.AddComponenet’ for the argument list ‘(badGuy)’ was found’
var gol = Instantiate(tokenType1, new Vector3(this.transform.position.x, this.transform.position.y, this.transform.position.z-1.5), Quaternion.Euler(270,0,0)) as GameObject;
var bgScript: badGuy = GetComponent(badGuy);
bgScript.quad = "right";
gol.AddComponent(bgScript);
I would be rolling if I could figure these two data centric code issues out.
Tried this to no avail either:
var gol = Instantiate(obj, new Vector3(this.transform.position.x, this.transform.position.y, this.transform.position.z-1.5), Quaternion.Euler(270,0,0)) as GameObject;
gol.AddComponent("badGuy");
gol.GetComponent("badGuy").quad = "left";
EDIT:
Okay I got the above to work and I don’t know why but removing the quotes on the GetComponent did it?!
function SpawnObject(obj : GameObject) {
var gol = Instantiate(obj, new Vector3(this.transform.position.x, this.transform.position.y, this.transform.position.z-1.5), Quaternion.Euler(270,0,0)) as GameObject;
gol.AddComponent("badGuy");
gol.GetComponent(badGuy).quad = "right";
}