Why is this If statement ignored?

I’ve got an if statement in update that is supposed to move an object towards to the player. For some reason the condition is ignored. I know it should be getting set. I’ve tried Debug.Log, setting a break point. Nothing shows the condition to be firing.

I set movingItem to false in start, and then true when take button is clicked.

Thanks for any help.

using UnityEngine;
using System.Collections;

public class btn_script : MonoBehaviour {

	// Player

	public GameObject player;

	public GameObject btn;
	public GameObject highlightBtn;

	private bool highlighted;

	private GameObject highlightedID;
	
	private GameObject recievedItem;
	private bool movingItem;

	// Use this for initialization
	void Start () {

		highlighted = false;
		movingItem = false;

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

		if (highlighted && highlightedID == null) {
			
			highlightedID = (GameObject)Instantiate (highlightBtn, new Vector3 (btn.transform.position.x, btn.transform.position.y, 0f), transform.rotation);
			
		} else {

				Destroy (highlightedID);

		}

		if (movingItem) {

				float t = 0;
			 	t += Time.deltaTime / 1f;
				recievedItem.transform.position = Vector3.Lerp (recievedItem.transform.position, transform.position, t);

		}


		// End update
	}

	void OnMouseDown()
	{

		// Left clicked menu button
		if (Input.GetMouseButton (0)) 
		{
			if(name.Contains("close"))
			{

				// Remove hover from all menu buttons
				GameObject[] objects = GameObject.FindGameObjectsWithTag("floor_item");

				foreach(GameObject item in objects)
				{
					
					if(item.GetComponent<HoverMenu>() != null)
					{

						item.GetComponent<HoverMenu>().clear();
						
					}
					
				}

				// End if button close clicked
			}else if(name.Contains ("take"))
			{

				// Add the item to the player
				recievedItem = player.GetComponent<Character>().return_last_clicked_item();
				player.gameObject.GetComponent<Character>().recieve_item();

				// Remove hover from all menu buttons
				GameObject[] objects = GameObject.FindGameObjectsWithTag("floor_item");
				
				foreach(GameObject item in objects)
				{
					
					if(item.GetComponent<HoverMenu>() != null)
					{
						
						item.GetComponent<HoverMenu>().clear();
						
					}
					
				}
			
				Debug.Log ("Seting Move True");
				movingItem = true;

				// End take clicked
			}
			
			
			// End left clicked menu button
		}


	}

	void OnMouseOver()
	{

		// count all opened menues
			
		highlighted = true;

		// End OnMouseOver
	}

	void OnMouseExit()
	{
		
             highlighted = false;

	}

}

This part makes no sense:

     if (highlighted && highlightedID == null) {
         
         highlightedID = (GameObject)Instantiate (highlightBtn, new Vector3 (btn.transform.position.x, btn.transform.position.y, 0f), transform.rotation);
         
     } else {

             Destroy (highlightedID);

     }

When “not” highlighted you try to destroy “highlightedID” every frame. When highlighted turns true you would instantiate an object which would destroyed immediately the next frame since your combined condition is false again (since highlightedID is no longer null). So every other frame you would constantly instantiate and destroy the object.

You most likely want the conditions to be:

if (highlighted && highlightedID == null)
{
    highlightedID = (GameObject)Instantiate (highlightBtn, new Vector3 (btn.transform.position.x, btn.transform.position.y, 0f), transform.rotation);
}
else if (!highlighted && highlightedID != null)
{
    Destroy (highlightedID);
}

The only reasons why non of the two cases execute would be:

  • The script is not attached to any object in the scene
  • The gameobject or one of it’s parents are not active so Update doesn’t run at all
  • The script component itself is disabled.