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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Update function is not getting called

Discussion in '2D' started by Amal_Joy, Jun 4, 2015.

  1. Amal_Joy

    Amal_Joy

    Joined:
    Jun 3, 2015
    Posts:
    4
    I have been trying to create my first game (a basic single player pong type game)
    . Capture56.JPG
    once the "blue slab" misses the ball, the ball gets eliminated and a new ball is created at x+2 of slab.
    . Almost everything went fine but I am having problem with "re spawning".
    .
    I coded such that the new ball created starts bouncing after we initiate bouncing by pressing left mouse button.( up and down arrows are used to control slab)
    .
    But after re spawning, I could move slab with arrow keys but ball does'nt start bouncing even after pressing mouse button.
    .
    I have used following 3 c# files for the project....
    ball.cs
    score.cs (its not for score....its for interaction with dead zone)
    realscore.cs (for scorring)
    .
    I have checked with debugging and found that once " void OnTriggerEnter(Collider other)" funtion in score.cs
    is called, "void Update ()" function in ball.cs is never called again.
     

    Attached Files:

  2. Wrymnn

    Wrymnn

    Joined:
    Sep 24, 2014
    Posts:
    352
    Few of the reasons your Update isn`t being called are:

    • You do not inherit from MonoBehaviour (Which I see you do)
    • Your script is not attached to object which is on active scene
     
    MIT-FAST-Lab and Amal_Joy like this.
  3. Amal_Joy

    Amal_Joy

    Joined:
    Jun 3, 2015
    Posts:
    4
    I am completely new to unity.... So can you please help me to fix it??? thank you..
     
  4. Leo-Yaik

    Leo-Yaik

    Unity Technologies

    Joined:
    Aug 13, 2014
    Posts:
    434
    Hi

    Does the ballpref (I assume it's a prefab) has the 'ball.cs' script attached?

    One suggestion I would like to make is instead of destroying the game object and create a new instance, why not just move ballx to the initial position?
     
    Amal_Joy likes this.
  5. Wrymnn

    Wrymnn

    Joined:
    Sep 24, 2014
    Posts:
    352
    Hi, I am currently at work but I can help a bit.

    Well, basically, from the points I have mentioned

    • You do not inherit from MonoBehaviour (Which I see you do)
    • Your script is not attached to object which is on active scene
    You meet the first point, since your classes all inherit from MonoBehaviour, e.g.:

    Code (csharp):
    1.  
    2. public class realscore : MonoBehaviour
    3.  
    The second point is, when you are in Unity Editor, the script which should run Update methods, should be attached to GameObjects which you have on your current scene/level.

    The thing I see is that you are using Debug.Log(), don`t use that, I didn`t use C# outside of Unity engine, but I think this is console(cmd) output when you are making some native C# application.

    Use print() function instead. s.g.

    Code (csharp):
    1.  
    2. print("Text I want to write to Unity console");
    3.  
     
    Amal_Joy likes this.
  6. Amal_Joy

    Amal_Joy

    Joined:
    Jun 3, 2015
    Posts:
    4
    thanx sir
     
    Wrymnn likes this.
  7. HALXP

    HALXP

    Joined:
    Sep 20, 2018
    Posts:
    1
    Or you've wasted time debugging like I just did, just to find out you called it "Udpate()" instead of "Update()" :p
     
    JustinBLDang and bnandrade like this.
  8. HKIGG

    HKIGG

    Joined:
    Oct 9, 2018
    Posts:
    1
    One more thing to pay attention to is, if your Component is not enabled (not ticked in the inspector) then the Update() method won't be called
     
  9. Offlein

    Offlein

    Joined:
    Nov 19, 2016
    Posts:
    1
    Following up on
    Udpate()
    vs
    Update()
    make sure the "u" in
    Update()
    is capitalized too. :(
     
  10. mretskn

    mretskn

    Joined:
    Mar 8, 2017
    Posts:
    2
    This solved my problem, thanks.
     
    Foxsocks79 likes this.
  11. useralpha

    useralpha

    Joined:
    Jun 19, 2017
    Posts:
    8
    Help! I'm not new to unity (although I'm not a pro either) and my Update() function is not running. The object is instantiated during runtime. As far as I can tell, the object and component are active and enabled. The code is something like this:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ScriptThing: MonoBehaviour
    7. {
    8.    //variables declared here
    9.  
    10.     void Update()
    11.     {
    12. print("Update ran.");
    13. }
    14. }
    Any ideas for what could be the problem?

    Edit: Start() runs successfully, and the code also receives function calls from UI button clicks successfully.
     
    Last edited: Jun 30, 2020
  12. MalteBernhardsson

    MalteBernhardsson

    Joined:
    Oct 11, 2019
    Posts:
    1

    @useralpha I've got the same problem and I saw that you also use UnityEngine.UI. I've got my script on a UI element and it may have to do with that, though I don't know how to get around it yet.
     
  13. Agent11196

    Agent11196

    Joined:
    Nov 7, 2020
    Posts:
    1
    the same happened to me having no option I tried using FixedUpdate() instead of Update() and it worked
     
  14. vfontoura

    vfontoura

    Joined:
    Oct 9, 2014
    Posts:
    40
    Hello ppl, I'm having the same problem with only one script Like Agent11196 changing to FixedUpdate() does help, but there is nothing to do with UnityEngine.UI since in my case I'm not using it, anyways I reposted this threde here > Unity 2020.3.5f1 Not Calling Update - Unity Forum since this one is from 2015 cheers.
     
    Orlik likes this.
  15. FelixNicol

    FelixNicol

    Joined:
    Jul 18, 2021
    Posts:
    3
    Thank you! I had a disabled script.
     
  16. firebird721

    firebird721

    Joined:
    Jun 8, 2022
    Posts:
    73

    amazing

    id didnt notice my fixedUpdate didnt have a big F...

    F---

    lol

    its werrd there is no comment for error - i just understand there are lots of function that just not being called

    good learning

    thx