Reputation: 23
I had to write a code in the enemy script that when they collides with a bullet damage is taken. Now I want to change this int, (which defines the damage of the bullet) but how? I am getting errors.
enemy script:
public int bulletdamage;
public void dead()
{
Destroy(gameObject);
}
public void OnCollisionEnter(Collision collision)
{
if (collision.collider.CompareTag("bullet"))
{
Debug.Log("loluhit");
healthenemy -= bulletdamage;
if (healthenemy <= 0f)
{
dead();
}
}
}
It's attached to the enemy, the enemy gets instantiated and gets a random number of live
Then there is the shootscript, where I want to change the int value, to let it be uncluttered:
public int bulletdmg;
void Start()
{
GetComponent<health>().bulletdamage = bulletdmg;
}
I get the error:
"NullReferenceException: Object reference not set to an instance of an object"
What to do?
Upvotes: 0
Views: 454
Reputation: 4283
If you are getting this error on the GetComponentInParent
line it's because the parent doesn't have the healt
script.
Make sure the object parent have this script attached.
Make sure that health
is typed correctly (classes used to start with capital letter).
Upvotes: 1