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
var tag_In_Map : GameObject[];
var Number_Of_tags : int = 0;
function Update ()
{
tag_In_Map= GameObject.FindGameObjectsWithTag("tag");
Number_Of_tags = tag_In_Map.length;
}
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 !
hmm i still dont get anything with these loops i will never understand them they look confusing
I made something like this
function Update ()
{
tag_In_Map = GameObject.FindGameObjectsWithTag("tag");
for each(var po : GameObject in tag_In_Map ) {
var v = po.GetComponent(PointScript);
Number_Of_tags += v.AmountOfPoints;
}
}
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
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):
function Update ()
{
Number_Of_tags = 0;
tag_In_Map = GameObject.FindGameObjectsWithTag("tag");
for each(var po : GameObject in tag_In_Map ) {
var v = po.GetComponent(PointScript);
Number_Of_tags += v.AmountOfPoints;
}
}
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.
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.