- Home /
Script detecting which object was clicked
Hello guys,
I have this problem. In my scene I have 20 objects, each with a script with a separate name that basically does the same thing. What it does, is that when the object is clicked, the script attached to it draws with NGUI a certain button.
The problem is that it draws the button for any clicked object, instead of the object which passes the conditions.
The script :
public class BattleCheck1 : MonoBehaviour {
public GameObject labelAttack;
public GameObject labelChallenge;
private int attack1;
private int challenge1;
// Use this for initialization
void Start () {
NGUITools.SetActive(labelAttack.gameObject,false);
NGUITools.SetActive(labelChallenge.gameObject,false);
attack1 = PlayerPrefs.GetInt("attack1");
challenge1 = PlayerPrefs.GetInt("challenge1");
}
// Update is called once per frame
void Update () {
if(MapMenu.checkChallenge == true)
{
if(attack1 == 1)
{
NGUITools.SetActive(labelAttack.gameObject,true);
}
if(challenge1 == 1)
{
NGUITools.SetActive(labelChallenge.gameObject,true);
}
}
}
The other objects have the scripts with variables attack2,3,4.. and challenge2,3,4 etc. For now I have only the attack1 = 1 and the rest of the variables are set to 0, but the button is drawn each time I click on any object.
How can I fix this?
Thank you
can I see your full code, actually I am unable to getting you exactly.
This is the code that does it.
I have this NGUI button.. and I want it to be drawn or not, based on which objects I clicked (based on the attack[n] variable). Right now it doesn't matter if attack1=1 and attack2=0 if I click on the second object it will still show me the button, even though it shouldn't.
Should I explain this better ? Can anyone let me know please ?
Actually seeing the full code (especially the part related to handling click) would be good.
As a side note - do you have 20 scripts? BattleCheck1 - BattleCheck20, with different variable names but the same code? If yes, then this is not the way to do it. I don't know how your game should work, but I strongly believe it is possible to use one script (maybe slightly modified) for all the objects.
I still don't get how it all works, but you should be able to get one script only by changing it to:
public class BattleCheck : $$anonymous$$onoBehaviour {
public int attack;
public int challenge;
public int number;
private CommonCrap zaScript;
// Use this for initialization
void Start ()
{
attack = PlayerPrefs.GetInt("attack" + number);
challenge = PlayerPrefs.GetInt("challenge" + number);
GameObject theCamera = GameObject.Find("$$anonymous$$apCamera");
zaScript = theCamera.GetComponent<CommonCrap>();
}
void Update()
{
if ($$anonymous$$ap$$anonymous$$enu.checkChallenge == false)
{
NGUITools.SetActive(zaScript.labelAttack.gameObject,false);
NGUITools.SetActive(zaScript.labelChallenge.gameObject,false);
}
}
void On$$anonymous$$ouseUp()
{
GameObject theCamera = GameObject.Find("$$anonymous$$apCamera");
CommonCrap zaScript = theCamera.GetComponent<CommonCrap>();
if(attack == 1)
{
NGUITools.SetActive(zaScript.labelAttack.gameObject,true);
}
if(challenge == 1)
{
NGUITools.SetActive(zaScript.labelChallenge.gameObject,true);
}
}
}
and then, after dragging your script to the game objects, you have to set their numbers from 1 to 20. This is of course a quick and probably not best solution, but much better that the one you're using.
Answer by darthbator · Sep 10, 2013 at 12:42 AM
Why not raycast from the camera at the mouse cursor position and then gather information based on what the raycast hit? That's how I handle all this manner of stuff. That way you can switch off data already on that gameObject (tags, layers, gameObject.name, or any attached script).