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

Check Element name from an array.

Discussion in 'Scripting' started by Thril3r, Jan 2, 2015.

  1. Thril3r

    Thril3r

    Joined:
    Dec 6, 2013
    Posts:
    19
    How can I check the name of an element from a game array?

    For example I have the gameobject "6" and I want to check with a script atached to gameobject "6" if the name of element 5 from "activator" is "score". If the name of element 5 is "score" to do something with the gameobject "6".
     

    Attached Files:

  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,683
    Code (csharp):
    1.  
    2. if(gameObject.name == "x")
    3. doSomething();
    4.  
     
  3. Thril3r

    Thril3r

    Joined:
    Dec 6, 2013
    Posts:
    19
    Check the image so you can understand better what I want. I want to check just one element from array not the entire object "activator".

    Thank you for answer!
     
  4. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,683
    Code (csharp):
    1.  
    2. for(int i = 0; i < array. Length; i ++){
    3. If(array[i].gameobject.name == "6")
    4. doSomething();
    5. }
    6.  
     
    Last edited: Jan 3, 2015
  5. Elson_Ng

    Elson_Ng

    Joined:
    Jan 3, 2015
    Posts:
    4
    Code (CSharp):
    1.  
    2.  
    3. var activator : GameObjects[];
    4.  
    5. for(int i = 0; i < activator.Length i++){
    6.         if(activator[i].name == "score"){ // Check if current index is 5
    7.                 activator[i].DoSomething(); // Do something with gameObject 6
    8.         }
    9. }
    10.  
     
  6. Thril3r

    Thril3r

    Joined:
    Dec 6, 2013
    Posts:
    19
    Sorry but I am new in programming.... but I get some errors :-/
    CS1002; Expecting ";" and unexpected symbol '.' .....

    What do I have to change to make it work?

    Code (JavaScript):
    1. {
    2.  
    3.         for(int I = 0; I < array. Length; I ++){
    4.             If(array[i].gameObject.name == "Score")
    5.                 CircleCollider2D.enabled = true;
    6.         }

    Btw if this does matter this is the script from the activator.

    Code (JavaScript):
    1. function Start()
    2. {
    3. gamearray = new GameObject[10];
    4. InvokeRepeating("ChangeArray", Random.Range(5, 15), Random.Range(5, 15));
    5. for(var i : float = 0; i < 10; i++)
    6. {
    7.     var chooseRandomly : boolean = (Random.value < 0.5);
    8.     if(chooseRandomly == true)
    9.    {
    10.        gamearray[i] = score;
    11.    }
    12.    else
    13.    {
    14.        gamearray[i] = death;
    15.    }
    16. }
    17. }
    18. function ChangeArray()
    19. {
    20. for(var i : float = 0; i < 10; i++)
    21. {
    22.     var chooseRandomly : boolean = (Random.value < 0.5);
    23.     if(chooseRandomly == true)
    24.    {
    25.        gamearray[i] = score;
    26.    }
    27.    else
    28.    {
    29.        gamearray[i] = death;
    30.    }
    31. }
    32. }
    I want to make every gameObject to check his element to get an effect something like this.
     
  7. Elson_Ng

    Elson_Ng

    Joined:
    Jan 3, 2015
    Posts:
    4
    You dont need to put array.gameObject again if im not wrong since array[pos] is a game object.

    Code (JavaScript):
    1. for(var i : int = 0; i < gamearray.Length; i++){
    2.        if(gamearray[i].name == "Score"){
    3.              gamearray[i].GetComponent(CircleCollider2D).enabled = true;
    4.        }
    5. }
    P.S What are scores and deaths? Strings ? or GameObjects?
     
  8. Thril3r

    Thril3r

    Joined:
    Dec 6, 2013
    Posts:
    19
    Thank you for answers, I will test the script when I will get back at home.
     

    Attached Files:

    • Help.jpg
      Help.jpg
      File size:
      604.2 KB
      Views:
      694