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

Finding all objects with tag and getting access to their variable

Discussion in 'Scripting' started by i3artyy2222, Nov 10, 2015.

  1. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    Hi all,
    I want to find all objects with certain tag and then get component point, because there are many of these object with this tag but some of them have different value of points given, so i want to find all objects with this tag and then calculate all points that then can give.

    Here what i made
    Code (JavaScript):
    1.  
    2. var tag_In_Map : GameObject[];
    3. var Number_Of_tags : int = 0;
    4.  
    5. function Update ()
    6. {
    7. tag_In_Map= GameObject.FindGameObjectsWithTag("tag");
    8. Number_Of_tags = tag_In_Map.length;
    9. }
    10.  
    This finds all the objects but what i want to make is to get access to all these objects with this tag get access to each of their script called Point and inside that script get access to variable int ValuePoint, so if there will be 2 objects with same tag 1 has valuePoint =1 and another object ValuePoint = 2 then it shows 3 [add all the ValuePoint variable from all the object with the same tag

    How can i make this guys :? I tried ("tag").GetComponent(PointScript); but it said
    'GetComponent' is not a member of 'UnityEngine.GameObject[]'.
    So i dont know how to make this
    Thanks for helping guys !
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
  3. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    hmm i still dont get anything with these loops i will never understand them they look confusing
    I made something like this
    Code (JavaScript):
    1.  
    2. function Update ()
    3. {
    4.              tag_In_Map = GameObject.FindGameObjectsWithTag("tag");
    5.              for each(var po : GameObject  in tag_In_Map ) {
    6.                  var v = po.GetComponent(PointScript);
    7.                  Number_Of_tags += v.AmountOfPoints;
    8.              
    9.              }
    10. }
    But when i play the game it just keeps counting it up, i just want it to calculate all ''AmountOfPoints'' variable in all of the objects with tag ''tag'' add them together so if there are 2 objects 1 has AmountOfPoints=1 and another has AmountOfPoints= 2, all i want is to add them together so its 3 and then when it gets destroyed it just takes away from 3 for example if you destroyed object 2 that has AmountOfPoints=2 then it takes away this from the 3 so it will be 1 [Basically i want to make a score 210/210 and when you destroy all objects with this tag it will go lower and lower until it reaches 0 and you destroy all objects with the same tag


    EDIT :: I could make ''provisional' way around this by using different tags and if its tag 2 then add 3 points if tag1 then add 1 point etc etc but with this foreach it would be easier because all i will have to change is to change AmountOfPoints in the object with tag 'tag' script and it will be done + i want to learn and understand them loops better so i could make something again later on from them
     
  4. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    I tried ' stoping' this 'loop' by adding break and return at the end but seems it didnt work :/
     
  5. Lentaq

    Lentaq

    Joined:
    Apr 8, 2015
    Posts:
    57
    You need to take all that stuff out of your Update() and put it in its own custom method, otherwise it'll just keep counting up every update. Then you need to have some way of calling that method to do a recount when an object is destroyed. That would be much cleaner than running that "tag" search and for loop every single update.

    However, the reason your score keeps going up, is because your Number_Of_Tags in update is not being reset to 0 at the start of each update, so it just keeps adding onto itself. You could just do this to keep it from "spilling over":

    Code (JavaScript):
    1.  
    2. Code (JavaScript):
    3.  
    4.    
    5.     function Update ()
    6.     {
    7.                  Number_Of_tags = 0;
    8.                  tag_In_Map = GameObject.FindGameObjectsWithTag("tag");
    9.                  for each(var po : GameObject  in tag_In_Map ) {
    10.                      var v = po.GetComponent(PointScript);
    11.                      Number_Of_tags += v.AmountOfPoints;
    12.                
    13.                  }
    14.     }
    15.  
    16.  

    That's pretty inefficient to have all that redoing itself over and over every update, though.

    Note: I'm assuming that the Number_Of_tags variable is your score counter since you are adding the v.AmountOfPoints to it each time.
     
  6. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    Yes it is score counter number of tags is a number of all these objects with the tag in the begining of game and i dont wanna use it again but i wanna get access to points inside all these objects and get points and then add to number of tags and then later on when they get destroyed get access again and take away this from another variable which will be == to number of tags but this one will change so when it reaches 0 the map is over.