The Update Function is Not Working in my code

It was working fine until now
The update function is not working and I don’t have any Idea
I checked some Forum posts and many people had this problem and either they made Spelling errors like Udpate() instead of Update(), They didn’t use MonoBehavior, and fixed update solved their problemfor many

But for me None of those relate
Update works some times and don’t most of the times
Pls can anyone let me know

Code Link ( Code is little too big ) : Code - Pastebin.com
Pls help
@MelvMay I Think You Can Help me

When Ctrl F’ing the code you posted, there is not even an Update function in there at all. Do you mean FixedUpdate?

Oops,
Really sorry for the inconvenience
Updated the link

1 Like

@Yoreki
I was testing Different Ways and Forgot to change it Back to Update,
It is both, Both The update and the fixed update are not working properly

Are there any errors?
Also,

this sounds a lot like the Update method is working fine, and the code you have in the update method simply does not do what you expect?
If you do a Debug.Log at the start of the Update method, does it really skip executing that debug log in some frames?
If not, your problem has nothing to do with the MonoBehaviour Update at all

I Had a Debug.Log in the Update and it did not work well,
for the first 5 seconds the debug .log wasn’t working and it worked for 10 seconds still not consistent and was stuttering and after 5 seconds it stopped working permanently. This Happened when i changed my code

The code where Update was working : https://pastebin.com/wDqMnS8k
I think that it kinda relates to performance or the code was using too many resources and the update wasn’t working
it is my thoughts and not confirmed

are you sure you had the debug log in the very first line when testing, and not after the

        if (target == null)
        {
            return;
        }

?
Could you post a screenshot of the console output for the case of having a debug log in the very first line?
If Update really stops working because of performance reasons, then the entire application should freeze…
Does Unity slow down and freeze?

just wondering, does the problem persist if you comment out the InvokeRepeating’s?
Why do you use InvokeRepeating anyway instead of just letting an Update method and a time counter handle your repeating events?

1 Like

I did put the debug.log after the return thing
is that the problem???
Pls let me know

return will interupt the method, everything written after return wont be executed
it essentially means the method is complete and ready to return its resulting output value, in the case of a void function, there is simply no output

in other words, every time your target is null, your update method will cancel and do nothing

2 Likes

OK
Reallly thanks
I really Didn’t know what to do
I did not know that return does that
I made some Debug.Log statements and decided that my Update wasn’t working
Thanks,
Dev

1 Like

I dont mean this in a mocking way at all, but you specifically put it there right? What did you think it does? :smile:

1 Like

I got a sense from the other thread that we had that you’re in a little over your head. Programming can be pretty simple, but you have to at least understand the fundamentals of code flow and data flow. Without that you are really going to have a rough time.

Check these guys out, and see the bottom of this post for how to do tutorials properly. If you do not do them properly, you might as well just not do them because you will get no benefit.

You can also go for any basic beginner C# tutorial you can find on Youtube.

Imphenzia / imphenzia - super-basic Unity tutorial:

https://www.youtube.com/watch?v=pwZpJzpE2lQ

Jason Weimann:

https://www.youtube.com/watch?v=OR0e-1UBEOU

Brackeys super-basic Unity Tutorial series:

https://www.youtube.com/watch?v=IlKaB1etrik

Sebastian Lague Intro to Game Development with Unity and C#:

https://www.youtube.com/watch?v=_cCGBMmMOFw

How to do tutorials properly:

Tutorials are a GREAT idea. Tutorials should be used this way:

Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That’s how software engineering works. Every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly. Fortunately this is the easiest part to get right. Be a robot. Don’t make any mistakes. BE PERFECT IN EVERYTHING YOU DO HERE.

If you get any errors, learn how to read the error code and fix it. Google is your friend here. Do NOT continue until you fix the error. The error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost.

Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there’s an error, you will NEVER be the first guy to find it.

Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

1 Like

I didn’t take it that way
actually I Followed Brackeys Tutorial while doing this until a certain point
I actually don’t know what return does

I actually Don’t need these because I am not a beginner
Thanks

I don’t want to sound rude but the “return” statement is kind of super basic in any programming language. If you don’t know what it does you should definitely check the videos that @Kurt-Dekker posted above.

6 Likes