- Home /
Multiple Method Calls
Hello so i currently have a question about my hitCollider Method
public void Check(Vector3 myCenter, float myRadius) {
Collider[] hitColliders = Physics.OverlapSphere(myCenter, myRadius);
int i = 0;
if(i < hitColliders.Length)
{
Plains = GameObject.Find("Plains");
Tile t = (Tile)Plains.GetComponent("Tile");
t.AdjustCurrentValue1(+1);
}
}
What its doing right now is checking to see if there are any objects within the Spheres radius and if so, it calls a method from another script and adjusts its variable. Well all this can do right now is adjust the variable of one script. Now say there are multiple objects in the spheres radius which there will be, how could i adjust the code so that it calls the method from each object and adjusts each ones variable. I was thinking of adjusting the GameObject.Find to something like tag.Find(if theres even a command for that) so that it would search for all objects with that tag. But I'm not sure if there is such a command. I currently have it working with just one object but im going to need multiple objects since what is going to be happening is that whatever tiles are in its radius are going to light up blue to let the player know his movement area. So this code has to adjust the variable in each of the tiles so that they can light up blue.
Why not just put a trigger on the change color blocks, then call a change in it's own local variable OnTriggerEnter, and change it back OnTriggerExit?
Thanks but I tried that, the ones already inside the radius wouldn't change color, I'm guessing because they didn't enter the area, they were already in it.
Use OnTriggerStay ins$$anonymous$$d of OnTriggerExit, then, or have you tried that as well?
No i haven't tried that, that might work though. But i got the script running correctly though now, Thanks for the help!
Answer by robertbu · Jan 11, 2014 at 04:30 AM
I have to guess a bit here. I'm assuming that your OverlapSphere() call is finding object the 'Tile' script attached. If so, you can do something like:
Collider[] hitColliders = Physics.OverlapSphere(myCenter, myRadius);
foreach (Collider col in hitColliders) {
Tile t = col.GetComponent<Tile>();
if (t != null) {
t.AdujstCurrentValue1(1);
}
}
I don't know what the '+' is the '+1' is all about. In addition, don't use the string version of 'GetComponent'. Use the generic version as I've done here.
Yes for every object in its radius which will be a tile, they will all have the Tile script attached. And Thank you i can see how this will work but the way it is right now unity is saying: Type Tile' does not contain a definition for
AdujstCurrentValue1' and no extension method AdujstCurrentValue1' of type
Tile' could be found i tried messing around with the parameters at the end of GetComponent(); but i could figure it out, any ideas?
$$anonymous$$ake sure the 'AdjustCurrentValue1()' method is public and that the name matches exactly to what you are using here. Note I compiled that bit of code (including a quick Tile script with an 'AdjustCurrentValue1()' method) to be absolutely sure it would compile.
Yep i see the problem AdjustCurrentValue1() was misspelled, it was spelled AdujstCurrentValue(); facepalm It works flawlessly now thank you!
Your answer
Follow this Question
Related Questions
How do I get a variable from another script and share the value of that variable between scripts? 1 Answer
Problem with var in C# (Vector3, transform and similiar) 1 Answer
cant change variable from a method? timer stays at 0! 1 Answer
Calling a variable based on a variables name? 1 Answer
Scriptable object variable not updating when changed, only when game stopped 1 Answer