- Home /
Update Function not working within a script inside an instantiated prefab!
Hi!
[Context]:
I'm trying to create an item pick up system for my game where when the player enters a certain OverlapRadius, a previously disabled "UI" game object parented to the dropped item prefab is enabled (and conversly disabled when the player exits the overlap radius). Now the "UI" game object contains a script that tries to do two things:
Modify the transform.rotation of the "UI" game object every frame (so that it stays upright no matter the orientation of the item prefab).
Makes a referenced, child "canvas gameObject" transform.lookAt the main camera every frame(So that the item pick up letter indicator 'E' is always oriented to the game screen-indicating to the player, there's an item that could be picked up)
[Problem]:
The Update function simply doesn't get called inside the instantiated prefabs! The Canvas is enabled correctly but the lookAt() doesn't work. Tried using a bunch of Debug.Log()s all over the script and came to understand that the update function just doesn't get called. But this problem, for some reason, only occurs when I try to use the enable() method I wrote inside my ItemObject script (That sits on the dropped item prefab) that's responsible for enabling/disabling and only enabling/disabling the childed "UI" gameObject. When I just drag in the prefab from my asset menu directly into the scene, and set it's setActive() state to true, the LookAt() and everything work just fine! [Code]:
public class ItemPickUpUiBehavior : MonoBehaviour { ///Works public Canvas uiCanvas; public bool UiEnabled = true; private Transform cameraTransform;
///Works
private void Awake()
{
cameraTransform = FindObjectOfType<Camera>().transform;
}
/// DOESn't WORK
void Update()
{
if (UiEnabled)
{
transform.rotation = Quaternion.Euler(-transform.parent.rotation.eulerAngles);
uiCanvas.transform.LookAt(cameraTransform);
}
}
///Works
private void OnEnable()
{
UiEnabled = true;
}
///Works
private void OnDisable()
{
UiEnabled = false;
}
}
I've been staring at this situation for over an hour and couldn't figure out what the problem is.
I tried looking through unity answers for similar situations but couldn't find anything helpful. Help would be much appreciated!!
Answer by eblavender · 4 days ago
Hi there!
Could it be that your gameobject/script is disabled? The Update function will only get called if the script and the gameobject the script is attatched to are both enabled. So in your example, you do not need the "UiEnabled" bool you have created either, because it wont be running that code anyway :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613061537im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
c# null script reference calls function with no error 0 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer
fully automatic gun script not working 0 Answers