Detecting for a variable from multiple potential scripts.
I have multiple objects, that deal damage with the colliders, although I get a lot of errors and null stuff.
Original code was simply:
////Grab damage of collision
damage = other.gameObject.GetComponent("Z95Projectile").damage;
damage = other.gameObject.GetComponent("MediumExplosion").damage;
damage = other.gameObject.GetComponent("SmallExplosion").damage;
Failed attempted solution:
////Grab damage of collision
damage = other.gameObject.GetComponent("Z95Projectile").damage;
if (damage == null)
{
damage = other.gameObject.GetComponent("MediumExplosion").damage;
if (damage == null)
{
damage = other.gameObject.GetComponent("SmallExplosion").damage;
if (damage == null) {
//later
} } }
This answer sort of helped, but not completely, because i am trying to use multiple different scripts, not players. http://answers.unity3d.com/questions/644841/nullreferenceexception-object-reference-not-set-to-83.html
Answer by fafase · Nov 25, 2015 at 08:00 AM
The issue there is that you are using GetComponent with a string. This results a reference of type Component. That component reference still needs to be cast down tot he type containing the variables.
I would recommend looking into interfaces. In your case, you would declare a IDamageable interface with a Damage property.
public interface IDamageable {
int Damage { get ; set;}
}
Then all three scripts you are dealing with would implement the interface and you can use one line then.
IDamageable damageable = other.gameObject.GetComponent<IDamageable>();
if(damageable != null){
int damage = damageable.Damage;
}
This is just basic software design. Take a moment studying c# and interfaces. You should specify the errors when you look for an answer. "A lot of errors and null stuff" doesn't tell us much. Also you should try looking up said errors first. Just google them. Your case is quite a classic example of a problem where a debugger would help. If you get a null pointer you have to find out where the null pointer is. Which variable is null. As fafase so nicely explained, the GetComponent call returns a null pointer when the component is not available. Since you are not checking it for null but directly accessing a variable, the thing will throw an exception. https://unity3d.com/learn/tutorials/modules/beginner/scripting/monodevelops-debugger
http://docs.unity3d.com/ScriptReference/Component.GetComponent.html if you read the reference you'll see it explicitly says that you get a null reference when the component does not exist.
I linked to a different question with the same error... :\ http://answers.unity3d.com/questions/644841/nullreferenceexception-object-reference-not-set-to-83.html
The scripts that count damage are multiples and damage is the variable which is incremented on collision. So the way you have written script where declaring same variable name for multiple scripts makes it complicated to debug and leads to issues later on if declared at multiple places like getComponent script.
You should create only one script as a manager. Score$$anonymous$$anager and declare the required variables and use singleton pattern for the instance of the script and call it directly in other scripts rather than as a GetComponent.vraiable. ".
If you want to check where damage in all scripts where it is getting referenced and called. You can go to the script and right click on variable and click on go to declaration and click on find references. It will show you a list where the variable is called. You can enter a debug on collision.
Always remember to use some design pattern for scripts if you calling it from other scripts. for better debugging, execution and optimization.
using UnityEngine;
public class HUD$$anonymous$$anager : $$anonymous$$onoBehaviour { public static Score$$anonymous$$anager sInstance; public static int damage; void Awake() { sInstance= this; }
void Damage() { Score$$anonymous$$anager.sInstance.damage++; } } //call damage wherever script you want by "Score$$anonymous$$anager.sInstance.damage++;"
What I have working at the moment is : if (other.name == "thisname" ) { Damage = 7;}
Im not sure if this method will have negative performance later on. But its better than the code in the question.
Does GetComponent nowadays work with interfaces? It used not to... Only normal inheritance & base classes
It actually does from Unity5. It used to be that you had to cast and so on but no more. You still can't use it in the inspector since it is no $$anonymous$$onoBehaviour and cannot be serialised.