- Home /
affecting only one Prefab in Code. (script)
in my game I have units you click on and tell what to do (RTS). so far I've coded it so that the unit becomes aware of when I click it, then it starts paying attention to the terrain script in order to know where to go.
it seems ridiculous to have my units looking at a different script, and if the units were unique I could just send the data straight to their script using "gameObject.Find("Unit").GetComponent("UnitsScript").someFunction(bla bla bla);" BUT the problem is all the units are prefabs that have been instantiated. so I can't do that right? becuase they're all named the same thing. but that's really what I'd like to do instead of having the units go looking for their directions when I click on them.
anyway, if that doesn't make sense here's my code, you will see nothing is being passed to my unit script, the unit script is looking to the terrain script to see if I have clicked on it. take a look if you want. or if you have a way to pass info to just one of my prefabs that would be awesome! thanks!
here's that part of my code:
function be () //called when I click on the unit. {
while (true) { if (lookingforwhattodo) { Debug.Log ("looking for what to do"); hereiam = GameObject.Find("Terrain"); if (hereiam.GetComponent("TerrainScript").getHasPackage()) //if i clicked on the terrain. to tell it where to go { //find out where we're supposed to go and what we're supposed to do foundwhattodo = hereiam.GetComponent("TerrainScript").VoodooYouDo(); foundwheretogo = hereiam.GetComponent("TerrainScript").getDestination(); Debug.Log ("found where to go" +foundwheretogo); Debug.Log ("found what to do" + foundwhattodo); //put the new location in my array list //p = 0; places.Push(foundwheretogo);//places.Add(foundwheretogo); doing.Push(foundwhattodo);
//set back to null
hereiam.GetComponent("TerrainScript").setHasPackage();
if (hereiam.GetComponent("TerrainScript").deselect)
{
lookingforwhattodo = false;
}
}
}
if (places.length >=1 && doing.length >= 1) { if (doing[p] == "Go") { ......
So thats the Code in my Unit. Here's the Code in my TerrainScript:
var haspackage : boolean = false; var voodo : String; var destination2 : Vector3; var deselect : boolean = true;
function OnMouseDown() { if (voodo != "null") // if I've clicked my menu to tell the unit what to do (go, attack, etc.) { deselect = false;// the unit is looking at this script presumably. var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit; if (Physics.Raycast (ray, hit, 2500)) { destination2 = hit.point; haspackage = true; } }
else //if I'm just clicking on the terrain without having clicked on a menu Item first. { deselect = true; }
} function setVoodoo(thatudo:String) { voodo = thatudo; }
function VoodooYouDo() { return voodo; } function getDestination() { return destination2; } function getHasPackage() { return haspackage; } function setHasPackage() { haspackage = false; voodo = "null"; destination2 = Vector3(0,0,0); }
Answer by StephanK · Apr 27, 2010 at 06:20 PM
I didn't really get what your code is doing and I'm not sure what you want to achieve, but if you want to call a function of a specific instance based on the name you could make sure that every instance of your prefabs gets a unique name in your scene. Something like:
GameObject go = GameObject.Instantiate("MyPrefab");
go.name = GenerateUniquename();
Much easier would be to keep your units in Lists or arrays, so they can be easily accessed by a specific index.
Your answer
Follow this Question
Related Questions
Insert a prefab into scene in a script? 1 Answer
Spawn a prefab 1 Answer
Communicating between objects in the same prefab? 1 Answer
Random Script Failure 1 Answer
How to apply a texture to a prefab? 2 Answers