How to check if either one of the script exist
I have a script called laserHandler.cs. It's suppose to handle both the Player and the Enemy's laser. I need to get the laser damage from the scripts of Player and Enemy. laserHandler.cs is a component of both Player and the Enemy game objects. So I wrote this in laserHandler.cs
private void Start()
{/* Here we get the damage variable by accessing the parent's script */
if(tag == "Player")
{
damage = transform.parent.GetComponent<PlayerController>().GetDamage();
}
else
{
damage = transform.parent.GetComponent<enemyHandler>().GetDamage();
}
}
But then when I click play, the script fires an error saying NullReferenceException. This is caused since the parent has either one of the script "PlayerController" or "enemyHandler". Both these scripts have different functions and only GetDamage() and the damage variable are the same.
How can I tell laserHandler.cs that it will only have either one of the scripts?
I've looked into Access variable in parent script and How to check if a component exists on a gameobject but I don't know how to apply these in my case. Any help will be appreciated.
Your answer
Follow this Question
Related Questions
How to make questions & answers random in my script C#? 1 Answer
There is something wrong with C# syntactic sugar in Unity, and i want to know the reason 1 Answer
Is it Possible to Cancel an Async Operation Once it has Begun? 1 Answer
Cant Get Simple Boolean Statement to Work! 1 Answer
How do I resolve, get_isActiveAndEnabled can only be called from the main thread error 1 Answer