- Home /
All objects gets same val. But i dont want that
hi. i have two planets, with gravitational field, and some space ships. when space ships entering gravitational field, it must send +1 to gravitation script. it working, BUT! both gravitational fields gets +1. why?
private var gravScript : gravitation;
function OnTriggerEnter (planetGrav : Collider)
{
if(planetGrav.name == "grav")
{
gravScript = planetGrav.GetComponent(gravitation);
gravScript.unitsCount++;
}
}
Did you make sure, that both Planet Colliders aren't touching each other so they dont get triggered both at the same time?
they are about 140 unity units away from each other. it's planets) they can't be close)
Their sizes are relevant to that question as well as their locations. I$$anonymous$$O the only way to be 100% sure is to put logging in the OnTriggerEnter function so you can see how many times and in relation to what pair(s) of objects it is called.
when i changing name in second gravitation field from "grav" to any else, second gravitation field still gets vals like in first field. i think gravScript = planetGrav.GetComponent(gravitation); gets vars not only from from planetGrav collider, but from all objects that have my script(gravScript : gravitation) attached. is it possible?
upd: yes, i just attached this script to another object, and it geting all the same vals as original gravitation field.
So the next thing I would do is look at is how when and from where those unitsCount values are getting changed.
Ins$$anonymous$$d of simply incrementing them, have a function called incrementUnitCount
on the gravitation script which not only does the increment but also logs that it's been called (together with the value in question as well as the name of the object). $$anonymous$$ake sure nothing else is changing that value.
Answer by Shuher · May 12, 2015 at 11:54 AM
it works!!! before function incrementUnitCount in gravitation script, i used "static var" for unitsCount. so now i removed "static" and it works!!)))
Answer by Eudaimonium · May 12, 2015 at 01:03 PM
Can you post the code of your Gravitation script/class/struct?
Also, to what object is this script attached, I assume the spaceship?
It seems to me you marked the "unitsCount" as "static", so the same variable is shared between all instances of the class that have it.
You need to remove the "static" keyword from there, if you have it. If not, I assume we'll find the problem in Gravitation.