Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Start function not calling?

Discussion in 'Scripting' started by BubyMB, Jan 28, 2017.

  1. BubyMB

    BubyMB

    Joined:
    Jun 6, 2016
    Posts:
    140
    I have this code;
    Code (csharp):
    1.  
    2.     void Start(){
    3.         Debug.Log ("add");
    4.         if (isInventoryMain) {
    5.             InventoryNumber = InventoryManager.GetComponent<InventoryManager> ().SlotManagerAdd (this.gameObject);
    6.             Debug.Log ("added");
    7.         }
    8.     }
    9.  
    that calls another script's function. This is on every small cell and there is about 34 of them.
    Theoretically, it should say "added', 34 times as all 34 have isInventoryMain ticked, but It doens't.
    So I decided to add Debug.Log("add"); at the start and not even that gets called. I have the logs enabled in console so it is not that. so this means that the Start function is not calling at all?

    This is the code in the other script if you are wondering;
    Code (csharp):
    1.  
    2.     public int SlotManagerAdd (GameObject slot){
    3.         InventorySlotsList.Add (slot);
    4.         InventorySlots = InventorySlotsList.ToArray();
    5.         Debug.Log ("Added slot");
    6.         return InventorySlotsList.Count;
    7.     }
    8.  
    It doesn't say "Added slot" either.
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,340
    Is the script enabled, and on active gameobjects?
    Also check your console. You can turn of Debug.Log messages in the upper right of the console window, make sure you haven't done that.
     
    mahu, nikhilsha, Bunny83 and 3 others like this.
  3. sushil_unity334

    sushil_unity334

    Joined:
    Aug 19, 2019
    Posts:
    1
    I had this issue. The script was not enabled on GameObject. Spent two hours on searching the solution.
     
    mahu and Coldroom like this.
  4. Zeclown

    Zeclown

    Joined:
    Jul 27, 2014
    Posts:
    17
    I had the same problem because I was disabling the script after spawning it (same frame). This was intentional. Changed it to void Awake instead and now it works.
     
    Bunny83, Hozgen90 and jacobclm1510 like this.
  5. AnilND

    AnilND

    Joined:
    Jul 13, 2019
    Posts:
    3
    Thanks!
     
  6. SlinkyNY

    SlinkyNY

    Joined:
    Feb 5, 2021
    Posts:
    2
    I'm currently having this problem in Unity 5.4 where start function isn't being run. I have the script attached to a game controller but I don't see where you can enable or disable a script on that controller. Is there a tick box somewhere that you can activate or deactivate it? The game controller does have a tick box and it is activated.

    [UPDATE] I created another TEST script, attached it to the game controller, and just did a debug.log in the start to make sure it works, and that doesn't seem to be working either. This script did have a check box next to it that can be toggled on and off and I have it checked. I have animators attached to the other script so I think that's why the toggle box may not be there?
     
    Last edited: Feb 9, 2021
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,927
    If your component does not have a checkbox on it, that means it does not have any Unity callback methods such as Start(), Update(), FixedUpdate() etc... that care about the enabled/disabled state of the component.
     
    Bunny83 likes this.
  8. Coldroom

    Coldroom

    Joined:
    Dec 15, 2014
    Posts:
    2
    Aaaaargh same here! I was also making it worse for myself because Awake() was working but Start() wasn't.
     
  9. RehanBhutta007

    RehanBhutta007

    Joined:
    Sep 3, 2020
    Posts:
    9
    Same Hare, I forgot to enable my script.:):):),,,
     
  10. FiveDoIIar

    FiveDoIIar

    Joined:
    Feb 7, 2021
    Posts:
    1
    spend 3 hours searching about why get component gave me null, and realized I have lower case 's' for start.
     
    KING-REBEL likes this.
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,936
    Yeah, I've been there, I've done that. And I've done about 57,000 other ways of getting null reference errors.

    Here is my shortcut to hammering through nullrefs... hopefully it can save you time too!

    --------------------------

    The answer is always the same... ALWAYS. It is the single most common error ever.

    Don't waste your life spinning around and round on this error. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception
    - also known as: Object reference not set to an instance of an object

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.

    Here is a clean analogy of the actual underlying problem of a null reference exception:

    https://forum.unity.com/threads/nul...n-instance-of-an-object.1108865/#post-7137032
     
  12. timrfrench61

    timrfrench61

    Joined:
    Sep 16, 2021
    Posts:
    1

    I read all the the "forgot to enable the script" answers but this was the one that made me into retyping all my start and updates and that's when I realized I had Disabled the Canvas the script was attached to instead of just the dialog that it contained that was being referenced by the script. This unity takes a slightly different mindset and slightly different debug strategies than I'm used to (HTML, SQL). Definately stretches the mind.

    Thanks all! I'll be Back.
     
  13. Sunwer91

    Sunwer91

    Joined:
    Jul 13, 2016
    Posts:
    5
    I had the same issue, but everything was fine.
    My script was attached to the object, was enabled, had the MonoBehaviour class (sometimes can be an issue)...but the problem was other, simple, and frustrating at the same time.

    Script 1:

    Start()
    {
    Script2.MethodX();
    }


    Script 2:
    Start()
    {
    object = new Object();
    }

    MethodX()
    {
    object.SetValue();
    }

    =============
    The problem was...the MethodX was called on the Start of Script 1, before Unity calls the Start of Script 2, so the "object" was null. (it was difficult to figure it out, because sometimes it worked and sometimes it didn't)

    My solution: Use a coroutine and wait a few frames to call the MethodX on script 1.

    I hope if someone have a similar problem, this solution can work.
     
    stronginmath likes this.
  14. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,074
    A coroutine is unnecessary. We have the Awake and Start Unity messages for this very reason.

    Do self initialisation in Awake, and do anything that requires others objects to be ready in Start.
     
    PraetorBlue, ZO5KmUG6R and Bunny83 like this.
  15. abotrix

    abotrix

    Joined:
    Oct 21, 2015
    Posts:
    20
    I had this problem too until I realized that I was destroying it in the Awake trying to set up an Instance with Destroy and DontDestroyOnLoad.
     
  16. tokurekin8

    tokurekin8

    Joined:
    Feb 27, 2023
    Posts:
    1
    Same. I wrote "IEnumerable" instead of "IEnumerator" because of vscode autocomplete.

    Code (CSharp):
    1. private IEnumerator Start(){
    2.         Debug.Log("Begin");
    3.         yield return new WaitForSeconds(3f);
    4.         Debug.Log("End");
    5.     }
     
  17. Hozgen90

    Hozgen90

    Joined:
    Jan 20, 2021
    Posts:
    19
    Start() method sometimes can not set component references when it is instantiated. In general Awake method can solve such problems.
     
  18. stronginmath

    stronginmath

    Joined:
    Apr 23, 2023
    Posts:
    1
    Thanks alot man I had this problem
     
  19. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,074
    Read my response after theirs. Use Awake for self-initialisation, Start for initialisation dependant on other objects.

    A coroutine is 100% unnecessary.
     
    Bunny83 and Ryiah like this.