Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Update function not working.

Discussion in 'Scripting' started by edan734, Aug 17, 2020.

  1. edan734

    edan734

    Joined:
    Nov 3, 2019
    Posts:
    5
    Hi!
    Im having problems with a c# script right now. The Update function simply won't work. No matter what code i put in it, it won't work. I removed all of my code and replaced it with a Debug.Log just to test, but nothing happens.
    Code (CSharp):
    1. void Update()
    2.     {
    3.  
    4.         Debug.Log("abc");
    5.  
    6.     }
    The code won't run. The console is empty when i hit play. It works perfectly if i copy the code into another script in my project. The rest of the code in the script works as well. Is this supposed to be possible? What can possibly cause something like this?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,944
    • Can you show the rest of the file? Is this function accidentally inside another function perhaps (that is a common beginner mistake)?
    • Does the class name match the script name in Unity?
    • Does the class extend MonoBehaviour?
    • Is the script attached to an activated GameObject in the scene?
    • Is the component enabled?
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,195
    Is the script on an object in the scene? Is the script active? Is the GameObject active? Create an empty gameobject, throw the script on it. Does it run?

    @PraetorBlue is super fast! :D
     
    PraetorBlue likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,314
    Praetor is turbo-Praetor! But I wild add one more:

    is your console log/logwarning/logerror buttons checked (upper right corner of log window)?

    See here: I have 97 warnings and have abdicated my responsibility as a developer by turning them off:

    Screen Shot 2020-08-17 at 3.55.25 PM.png
     
    Last edited: Aug 17, 2020
    MaskedMouse and PraetorBlue like this.
  5. edan734

    edan734

    Joined:
    Nov 3, 2019
    Posts:
    5
    This is the whole script. I use it for saving the amount of coins collected as well as the current highscore. Everything works as intended except the Update function. Note that i used the Update function for updating highscores, but replaced the code with a Debug.Log for easier testing. All the other scripts work fine. The class name matches the script name, it is attached to an enabled gameObject and the script runs. Everything runs except Update().
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SaveScript : MonoBehaviour
    6. {
    7.    
    8.  
    9.     public int coins;
    10.  
    11.     public int highScore;
    12.  
    13.     void Awake()
    14.     {
    15.         LoadGame();
    16.     }
    17.  
    18.    
    19.     void Start()
    20.     {
    21.         DontDestroyOnLoad(transform.gameObject);
    22.        
    23.     }
    24.  
    25.  
    26.     void Update()
    27.     {
    28.  
    29.         Debug.Log("abc");
    30.  
    31.     }
    32.  
    33.     public void SaveGame()
    34.     {
    35.         PlayerPrefs.SetInt("Coins", coins);
    36.         PlayerPrefs.SetInt("Highscore", highScore);
    37.         PlayerPrefs.Save();
    38.         Debug.Log("Saved");
    39.      
    40.     }
    41.  
    42.     void LoadGame()
    43.     {
    44.         if (PlayerPrefs.HasKey("Coins"))
    45.         {
    46.             coins = PlayerPrefs.GetInt("Coins");
    47.             highScore = PlayerPrefs.GetInt("Highscore");
    48.             Debug.Log("Loaded");
    49.         }
    50.         else
    51.             Debug.LogError("No save data found");
    52.     }
    53.  
    54.    
    55.  
    56. }
    57.  
     
  6. edan734

    edan734

    Joined:
    Nov 3, 2019
    Posts:
    5
    Solved the problem! Just a stupid noob mistake. Since i use DontDestroyOnLoad(), i only have the gameObject and the script in one scene, my menu scene. I was editing another scene when i started playing, so of course the script wasnt active. The rest of the script outside of the update function is only used for saving, so i did not notice that it didnt work either. Thanks!
     
    Yoreki and PraetorBlue like this.
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,314
    You can actually do away with dropping this into the scene and just have it come into existence when you access it. That pattern is called a singleton and in Unity, here are some super-simple Singleton examples to take and modify:

    Simple Unity3D Singleton (no predefined data): <--- probably the one you want

    https://pastebin.com/SuvBWCpJ

    Unity3D Singleton with Prefab used for predefined data:

    https://pastebin.com/cv1vtS6G

    These are pure-code solutions, do not put anything into any scene, just access it via .Instance!

    If you did that then you would access it with:

    Code (csharp):
    1. SaveScript.Instance.SaveGame();
    The instant you attempt to use Instance that script will be created, and you never drag it into any scene... once created it just hangs around in your DoNotDestroy list.
     
    Last edited: Aug 18, 2020
    Yoreki likes this.