Scripting Error
I am trying to make a script to make my player defeat the enemy and I am getting errors when I run the scripts.
here is the first one:
#pragma strict
function Die (){
Destroy (gameObject);
}
here is the second one that uses the first one :
#pragma strict
function OnTriggerEnter (){
var enemy = transform.GetComponentsInParent (Enemy);
Enemy.Die();
}
Do any of you know how to fix this?
Capitalization counts -- Enemy
and enemy
refer to different things!
enemy.Die()
will work, provided that enemy
is pointing at something.
Enemy.Die()
won't compile because Enemy
is a class. Classes are like blueprints for objects -- an important distinction to understand.
I tried that and it says Assets/Die_On_Hit.js(5,15): BCE0019: 'Die' is not a member of 'UnityEngine.Component[]'.
Try GetComponentInParent ins$$anonymous$$d of GetComponentsInParent
Note: the difference is a "s" in GetComponent(s). The plural version returns an array, you just want the singular version.
Your answer
![](https://koobas.hobune.stream/wayback/20220612045726im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Boolean variable won't trigger in other script 1 Answer
Game Show Happy Birthday whenever Birthday Will Come 1 Answer
Wanting audio to pause on trigger enter 1 Answer
Audio play when enter trigger 1 Answer
How to get a public int equal the same number when added in all object that have the same script? 1 Answer