- Home /
Find a script on a villager at runTime JavaScript
I am currently creating an RTS game and as you can imagine I have many villagers on the screen at once. When it is time to build something the player must click a villager and then click on a building plot. I need the build plot to check for which villager has been licked otherwise all would try and build items. And this check is on the villager 'ClickToMove' script.
Now to currently I have the following code to check for a villager with that specific script activated:
function Start ()
{
villagerClick = clickToMove.FindGameObjectWithTag("villager");
}
However I know this doesn't work and with there being multiple villagers I cannot place one on the variable before game launch. So does any one have any ideas on how to fix this?
Answer by Kergal · Feb 08, 2013 at 04:47 PM
an idea - probably not the best would be to add the function OnMouseDown() to every villager script.
and attach a nice collider to the villagers (you prob. already have done that) .
void OnMouseDown(){
transform.GetComponent<WhateverScript>().WhateEverVariable/OrFunction;
}
obviously this is not the entire solution - but I am sure you can work something out with that.
Your answer
Follow this Question
Related Questions
Creating a file and writing into it? :p 1 Answer
Question Concerning transform.Rotate 1 Answer
Unity fire1 prefab in code 2 Answers