Health pickup script issue
I've used this code to try and create a health potion, however, it doesn't work and I get this error in the Console:
Assets/Scripts/Health potion.js(1,14): BCE0018: The name 'Health' does not denote a valid type ('not found').
You should be able to assign the health control script in the "Script" variable.
var Script : Health; //This allows you to place the Health script in the Inspector.
function OnTriggerEnter(){
Destroy(gameObject); // Destroys the health potion
print("Gained 10 Health");
Script.player_health +=10; // Script is the Health script we assigned in the Inspector.
}
Well, you either haven't got a Health.js class, or there's a problem with it.
Also, the way you have the collisions set up probably won't work. You need to link to a physics collider. (Sorry it's in C#, that's the language I use predo$$anonymous$$antly). You'd do something like this.
void OnTriggerEnter(Collider other) {
if (other.gameObject.tag == "hpPotion") {
// add to player's health
}
}
Answer by allenallenallen · Dec 28, 2016 at 02:48 PM
You got the source code from here, right: https://forum.unity3d.com/threads/health-pickup.101377/
If you're copying code from someone else, you better understand how the code works.
Furthermore, if you are going to modify the code you got from someone else, you better understand how EVERYTHING works.
You changed the original "Health Control" script name to "Health". This means you better have a script called Health.js somewhere in your folder. And you better make sure it has a public player_health variable. (In case you haven't noticed, the guy didn't post his "Health Control" script so you should've written your own. In your case, Health.js.)
Then you should think about the code given by the second guy and why it doesn't make much sense. It's destroying the health pickup object along with the "Health potion.js" script that's attached to it before it gives the health to "Health.js".
It should be like this instead:
print("Gained 10 Health");
Script.player_health +=10; // Script is the Health script we assigned in the Inspector.
Destroy(gameObject); // Destroys the health potion
@allenallenallen I understand the code completely and I have the player_health variable in the Health script. I think the error may be because the Health script is C# and HealthPotion is Js?
In that case, yes, the last one that's compiled will be able to see the previous compiled codes.
So I guess Health pickup is compiled first right now.
You can probably fix it by looking at this answer: http://answers.unity3d.com/questions/252865/accessing-a-c-field-from-javascript.html
$$anonymous$$ake sure Health is compiled first.
If that doesn't fix it though, it might be another problem.