Scripting these if/else statements to operate correctly

First here’s the code: `using UnityEngine;
using System.Collections;

public class AnimationTest : MonoBehaviour {

public GameObject leganim;

// Use this for initialization
void Start () {

}



	
	
	
// Update is called once per frame
void Update () {

	if (Input.GetMouseButtonDown(0)){
		GetComponent<MegaModifyObject>().enabled = true;
	
	}
		
	else{	if (leganim.GetComponent<MegaPointCache>().time >= 2) {	
			GetComponent<MegaModifyObject>().enabled = false;
		}
		
	}
	
		if ( Input.GetMouseButtonDown(0) && leganim.GetComponent<MegaPointCache>().time >= 2 ){
		GetComponent<MegaModifyObject>().enabled = true;
	}
		
		if (leganim.GetComponent<MegaPointCache>().time >= 5) {	
			GetComponent<MegaModifyObject>().enabled = false;
		}
	
	
		if ( Input.GetMouseButtonDown(0)){
		GetComponent<MegaModifyObject>().enabled = true;
	}
		
		if (leganim.GetComponent<MegaPointCache>().time >= 8) {	
			GetComponent<MegaModifyObject>().enabled = false;
		}
	
	
	//else{
		//GetComponent<MegaModifyObject>().enabled = false;
	//}
	/*if (leganim.GetComponent<MegaPointCache>().time >= 10) {
		GetComponent<MegaModifyObject>().enabled = false;
	
	}*/
}

}
`

Basically what is happening is I have three different scripts attached to a single object. With this AnimationTest script I am referencing the variable “time” in the MegaPointCache script and when that time is greater or equal to 2 seconds turn off the MegaModifyObject script. The MMO script starts out off; when I click anywhere it turns on and the MPC script proceeds to over 2 seconds (the developer has it increasing randomly to 6 decimal points; hence the greater than part) the MMO turns off. But when I try clicking to turn it back on and proceed to 5 seconds, it won’t turn on and I can’t even turn it on manually. I’m assuming this is due to the commands conflicting and since it is still over 2 seconds it won’t let it turn on even though I’m telling it to. I don’t know how to fix this, but I’m guessing it’s pretty simple (hopefully).

Any ideas on how to fix this? Thanks!

public GameObject leganim;
private float intervalTime = 2.0f;
private MegaPointCache theCache;
private MegaModifyObject theObject;

        // public accessor if you want another class to maybe change the interval time?
        public float IntervalTime
        {
            get { return intervalTime; }
            set { intervalTime = value; }
        }

        void Start()
        {
            // store this here so we don't have to keep calling get component (its expensive!)
            theCache = leganim.GetComponent<MegaPointCache>();

            // same with the object
            theObject = GetComponent<MegaModifyObject>();

            // start the coroutine on start up
            StartCoroutine(SomeFunctionName());
        }

        void Update()
        {

            if (Input.GetMouseButtonDown(0))
            {
                theObject.enabled = true;
            }

        }

        IEnumerator SomeFunctionName()
        {
            // this will loop like an update, you might want to replace true with a bool you have control of (to possibly stop this updating later)
            while (true)
            {
                yield return new WaitForEndOfFrame();

                if (theCache.time >= intervalTime)
                {
                    theObject.enabled = false;
                }
            }
        }

This is personally what i would do, i haven’t had any chance to test this at all so apologies if it doesn’t work or there are some syntax errors :slight_smile:

I have split what you had in the Update into a coroutine, i prefer using these 90% of the time, makes it neater and gives you more control (among many other benefits). I recommend you look into coroutines they are very useful.

http://docs.unity3d.com/Documentation/ScriptReference/index.Coroutines_26_Yield.html

So to give a brief explanation to what i changed

  • GetComponent calls: You should always avoid calling GetComponent inside an Update loop, it’s very expensive. Instead, assign these inside Start to an object that you can reuse.

  • intervalTime: I have added a variable called intervalTime, this is used to check against the MegaPointCache.time like you were before. Somewhere in your code you need to update this so it waits the correct amount of time (2, 5, 8, 10 etc) it avoids having numerous if statements for each seperate time.

Give it a try and let me know how it goes.