To set the scene:
I have a small game where you shoot coconuts at targets, you hit them, you fall down, you get something. Now, my question is, I have two scripts set up:
This one is used to knock the targets down when hit with the coconut, and then knocks the variable ‘washit’ up by one.
using UnityEngine;
using System.Collections;
public class targetCollisions : MonoBehaviour {
bool beenhit = false;
Animation targetroot;
public AudioClip hitsound;
public AudioClip resetsound;
public float resetTime = 3.0f;
public int washit;
// Use this for initialization
void Start () {
targetroot = transform.parent.transform.parent.GetComponent<Animation> ();
washit = 0;
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter (Collision col) {
if (beenhit == false && col.gameObject.name == "coconut") {
StartCoroutine("targetHit");
}
}
IEnumerator targetHit () {
AudioSource hitAudio = GetComponent<AudioSource> ();
hitAudio.clip = hitsound;
hitAudio.Play ();
targetroot.GetComponent <Animation> ().Play ("down");
beenhit = true;
yield return new WaitForSeconds(3);
targetroot.GetComponent <Animation> ().Play ("up");
AudioSource resetAudio = GetComponent<AudioSource> ();
resetAudio.clip = resetsound;
resetAudio.Play ();
beenhit = false;
washit ++;
}
}
Now, this next script also uses ‘washit’, and when washit equals three, it’s supposed to translate the object the script is attached to.
using UnityEngine;
using System.Collections;
public class victorycell : MonoBehaviour {
public int washit;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (washit >= 3) {
transform.position = new Vector3(20f, 10f, 15f);
}
}
}
The problem is, of course, that washit isn’t increasing in value despite me hitting the target. What have I done wrong?