Okay so while following a tutorial online I wrote a code so that when I walked up to my coconut and my hunger was less than 90 I would eat it (Make it disappear) and my hunger would go up by ten.
`#pragma strict
var health : float = 100.0;
var hunger : float = 100.0;
function Start () {
}
function Update () {
hunger -= Time.deltaTime/20;
if(hunger <= 5){
health -= Time.deltaTime/1;
}
if(health <= 0){
print("You Died.");
}
if(hunger >= 80){
health += Time.deltaTime/1;
}
health = Mathf.Clamp(health, 0.0, 100.0);
hunger = Mathf.Clamp(hunger, 0.0, 100.0);
}
function OnTriggerEnter (other : Collider) {
if(other.gameObject.name == ("CoconutCol") && hunger <= 90.0){
hunger += 10;
Destroy (other.gameObject.transform.parent.gameObject);
}
}`
It worked just fine. And then I wrote another script so when I walked up to a tree it would shake (I also made the animation) and the coconut would fall off the tree and not be part of it anymore and I could eat it.
#pragma strict
var coconut : GameObject[];
var trigger : GameObject;
function Start () {
}
function Update () {
}
function CFall1 () {
coconut[0].rigidbody.isKinematic = false;
coconut[0].transform.parent = null;
}
function CFall2 () {
if(coconut[1] != null){
coconut[1].rigidbody.isKinematic = false;
coconut[1].transform.parent = null;
}
}
function CFall3 () {
if(coconut[1] != null){
coconut[2].rigidbody.isKinematic = false;
coconut[2].transform.parent = null;
}
}
function Shake () {
animation.Play();
}
function Stop () {
animation.Stop();
}
Well for some reason, I can’t eat them anymore. And I don’t know what happened? Can anyone help me? It’s all in java.