Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

I need help with find gameobject

Discussion in 'Scripting' started by MG, Apr 5, 2015.

  1. MG

    MG

    Joined:
    Nov 10, 2012
    Posts:
    190
    I have object a, b, c, d, e, f, g
    All of them contain a bool named selected which is false as default, and all of them have the tag named "Tower"

    Now one of them has its selected bool = true but we dont know which one

    In a script, I have a variable "public Transform Tower"
    I want this variable to have the object that has selected = true;

    How do i do that
     
  2. lineupthesky

    lineupthesky

    Joined:
    Jan 31, 2015
    Posts:
    92
    Not sure if this is the best way, but I can tell you that you can store those objects in an array, and then use a for loop to check if any of those objects has the variable as true.

    Code (CSharp):
    1. private Transform Tower;
    2. public Transform[] objects;
    3.  
    4. IEnumerator Check()
    5. {
    6. for(int i =0; i < objects.Length; i++)
    7. {
    8. ScriptName script = objects[i].GetComponent<ScriptName>();
    9. if(script.selected)
    10. {
    11. Tower = objects[i];
    12. return;
    13. }
    14. yield return null;
    15. }
    16. }
     
  3. zoran404

    zoran404

    Joined:
    Jan 11, 2015
    Posts:
    520
    lineupthesky, your code is good, but you will probably have to perform this check often (probably in the Update function) and the GetComponent function is relatively slow, so instead of having an array of Transform, you should use an array of ScriptName and get the transform property from it. This way you can avoid using GetComponent.
    Use GameObject.FindGameObjectWithTag to find the objects in the Start function.
     
    lineupthesky likes this.
  4. lineupthesky

    lineupthesky

    Joined:
    Jan 31, 2015
    Posts:
    92
    Yes my code is no appopriate for any check which is needed to be performed constantly, use the logic with his advice, reach directly to the script itself. (y)
     
  5. Barachiel

    Barachiel

    Joined:
    Nov 25, 2012
    Posts:
    147
    In the script attached to the towers you could have a variable to contain a reference to the script you want to update. When that particular tower becomes selected, set the "public Transform Tower" variable directly from that tower's script.
    Just make the reference to the script public so you can drop it in.
    Something like this, though I apologize if I messed it up. Quite tired here. =P
    Code (CSharp):
    1. boolean selected = false;
    2. public ExampleScript exampleScript;
    3.  
    4. void Update()
    5. {
    6.     if(selected)
    7.     {
    8.         exampleScript.tower = this.transform;
    9.     }
    10. }
    Alternatively, you could use delegates and events.
     
  6. zoran404

    zoran404

    Joined:
    Jan 11, 2015
    Posts:
    520
    Barachiel, this would require to manually attach the ExampleScript to each tower.

    Both examples so far are executed constantly in the Update function, which is a bit unnecessary.
    Instead why don't you just save the Transform of the tower in the script that does the selecting, since it already knows which tower is selected?
     
  7. Barachiel

    Barachiel

    Joined:
    Nov 25, 2012
    Posts:
    147
    You could simply find the example script in the start function, no need to attach it.
    If there were only a handful of towers, it might be easier to have a public variable for it so you can just drag and drop the reference to the example script, saving you a getComponent call, which is what I was suggesting in my original post. =)