Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Update not being called?

Discussion in 'Scripting' started by TaleOf4Gamers, Aug 1, 2016.

  1. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    Hey everyone.
    Currently I have an odd situation where it (appears) at least that Update is not getting called at all.
    I will get straight to it and show some code as there is not much else to say.

    This is the offending Update that is not getting called even though the Start does.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraFacingBillboard : MonoBehaviour
    5. {
    6.     public Camera m_Camera;
    7.  
    8.     void Start()
    9.     {
    10.         if(m_Camera == null)
    11.             m_Camera = Camera.main;
    12.     }
    13.    
    14.     void Update()
    15.     {
    16.         transform.LookAt(transform.position + m_Camera.transform.rotation * Vector3.forward, m_Camera.transform.rotation * Vector3.up);
    17.         Debug.Log("Update Called!");
    18.     }
    19. }
    I create a canvas with this code. (The canvas is world space and has the Billboard look at script on it.)
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. namespace CarlH{
    5.     namespace Tooltips{
    6.         public class ItemTooltip : MonoBehaviour {
    7.        
    8.             public float heightOffset = 1f;
    9.             GameObject canvas;
    10.  
    11.             public void ActivateTooltip(){
    12.                 if(canvas != null)
    13.                     canvas.SetActive(true);
    14.                 else
    15.                     CreateCanvas();
    16.             }
    17.  
    18.             public void DeactivateTooltip(){
    19.                 if(canvas != null)
    20.                     canvas.SetActive(false);
    21.                 else
    22.                     CreateCanvas();
    23.             }
    24.  
    25.             void CreateCanvas()
    26.             {
    27.                 GameObject tooltipCanvas = (GameObject)Resources.Load("PickupCanvas");
    28.                 Vector3 spawnPos = new Vector3(transform.position.x, transform.position.y + heightOffset, transform.position.z);
    29.                 canvas = (GameObject)Instantiate(tooltipCanvas, spawnPos, transform.rotation);
    30.                 canvas.transform.SetParent(transform, true);
    31.             }
    32.         }
    33.     }
    34. }
    35.  
    I detect how to activate/deactivate the canvas using this script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. namespace CarlH{
    5.     namespace Tooltips{
    6.         public class DetectItem : MonoBehaviour {
    7.          
    8.             GameObject hitPickupCache;
    9.  
    10.             void Update () {
    11.                 RaycastHit hit;
    12.  
    13.                 if(Physics.Raycast(transform.position, transform.forward, out hit, 5f)){
    14.                     if(hit.transform.gameObject.tag == "Pickup"){
    15.  
    16.                         if(hitPickupCache != null){
    17.                             hitPickupCache.GetComponent<ItemTooltip>().DeactivateTooltip();
    18.                             hitPickupCache = hit.transform.gameObject;
    19.                         }
    20.                         else{
    21.                             hitPickupCache = hit.transform.gameObject;
    22.                         }
    23.  
    24.  
    25.                         hit.transform.gameObject.GetComponent<ItemTooltip>().ActivateTooltip();
    26.  
    27.                         if(Input.GetKeyDown(KeyCode.E)){
    28.                             Destroy(hit.transform.gameObject);
    29.                         }
    30.                     }
    31.                     else if(hitPickupCache != null){
    32.                         hitPickupCache.GetComponent<ItemTooltip>().DeactivateTooltip();
    33.                     }
    34.                 }
    35.                 else if(hitPickupCache != null){
    36.                     hitPickupCache.GetComponent<ItemTooltip>().DeactivateTooltip();
    37.                 }
    38.             }
    39.         }
    40.     }
    41. }
    The canvas spawns correctly and even funnier, it was actually working a few minutes ago.
    Tried restarting Unity so I dont know what it could be.
    (Im using Unity 5.4 official release)
     
    Last edited: Aug 1, 2016
  2. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    Is the script getting deactivated at anypoint? or the GameObject?
     
  3. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    Yes, it gets activated and deactivated at times. (The GO that is)
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,181
    Update won't get called on scripts that are attached to inactive gameobjects.
     
  5. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    I know. but even when I activate the GO again it doesnt run. It worked before but have no idea why it isnt now.
     
  6. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    You can use gameObject.activeInHierarchy to check if it should be updating. As long as the component, the gameobject, and all its parents are active, it will be updated.

    https://docs.unity3d.com/ScriptReference/GameObject-activeInHierarchy.html