Update function is not getting called

I have been trying to create my first game (a basic single player pong type game)
.


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.

2143146–141340–ball.cs (650 Bytes)
2143146–141341–score.cs (618 Bytes)
2143146–141342–realscore.cs (497 Bytes)

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
2 Likes

I am completely new to unity… So can you please help me to fix it??? thank you…

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?

1 Like

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.:

public class realscore : MonoBehaviour

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(), dont use that, I didnt 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.

print("Text I want to write to Unity console");
1 Like

thanx sir

1 Like

Or you’ve wasted time debugging like I just did, just to find out you called it “Udpate()” instead of “Update()” :stuck_out_tongue:

2 Likes

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

4 Likes

Following up on Udpate() vs Update() make sure the “u” in Update() is capitalized too. :frowning:

3 Likes

This solved my problem, thanks.

1 Like

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:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ScriptThing: MonoBehaviour
{
   //variables declared here

    void Update()
    {
print("Update ran.");
}
}

Any ideas for what could be the problem?

Edit: Start() runs successfully, and the code also receives function calls from UI button clicks successfully.

@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.

the same happened to me having no option I tried using FixedUpdate() instead of Update() and it worked

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.

1 Like

Thank you! I had a disabled script.

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