- Home /
Duplicate Question
Add Object to a Variable Only When In Range
The question is a little more complicated than what I could post. I have a cube (with mesh render turned off) that has "is trigger" turned on. Here is what is supposed to happen if there is a collision:
Answer by FL · May 25, 2013 at 10:47 PM
A - Did you really is entering in the Trigger functions? Put a debug log in the trigger function and look at http://unitygems.com/mistakes1/ , third topic.
B - enemy_health is static, so all instances and even non-instances have the same value, if you change one, you change all! Remove the "static" word.
A - That guide is REALLY helpful, thanks! Adding a Rigidbody and making it kinematic REALLY helped :) However, this now creates a NEW problem of making the enemy's attack range infinite for some reason (only when it's set to kinematic and gravity is turned off, if gravity is on and it's NOT kinematic then the attack range of the enemy works normally). But now I can attack and destroy individual enemies.
B - see, I thought it had to be static because for almost every other instance of accessing variables from other scripts I would get the compiler error "An instance of type [script with variable I am trying to access] is required to access non-static member [Name of variable]". But changing it to NOT be static worked perfectly!
As a general rule never, ever, use "static". It's that simple.
It's an advanced topic, and totally irrelevant. never use static unless (in the far future!) you truly understand it. It is irrelevant here in all events.
simply read this
http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
and this
http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Components.html
and also if needed learn how to use "dontdestroyonload" (see docs, or 100s of questions here)
Follow this Question
Related Questions
weapon collision with characters 1 Answer
Player take damage on collision with AI 1 Answer
Character controller mess up damage 3 Answers
Damage on collision problems... 0 Answers
Velocity data from OnCollisionEnter is delayed (incorrect) 3 Answers