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. Dismiss Notice

I need help...

Discussion in 'Scripting' started by NotSoCreative, May 13, 2020.

  1. NotSoCreative

    NotSoCreative

    Joined:
    May 11, 2020
    Posts:
    3
    I'm very new to coding and to unity. I'm currently doing the Creator Kit: Beginner code. Everythings worked fine until now. I'm supposed to write my first own script and to do this I need to create a new script file. They've included a template for this new script file in the Beginner Code tab on unity called "Create item Effect" When I press this I get a window to name the new script file. I name it according to the intructions they give me and press create. Then I get this error message.

    NullReferenceException: Object reference not set to an instance of an object
    NameWindow.OnGUI () (at Assets/Creator Kit - Beginner Code/Scripts/Editor/CreateScriptEditor.cs:178)


    have no idea what it means and no idea how to fix it. I can't progress without fixing it. Any help is much appreciated :)

    Thanks
     
  2. NotSoCreative

    NotSoCreative

    Joined:
    May 11, 2020
    Posts:
    3
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    Excellent!!! In any case, I have been working on a boilerplate response for these types of error questions:

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

    Comments always welcome.
     
  4. unity_9YfYLtsa36D2sA

    unity_9YfYLtsa36D2sA

    Joined:
    May 23, 2020
    Posts:
    2
    mine dont work either
     
  5. unity_9YfYLtsa36D2sA

    unity_9YfYLtsa36D2sA

    Joined:
    May 23, 2020
    Posts:
    2
    i neeeeed help
     
  6. elijahko

    elijahko

    Joined:
    Mar 26, 2018
    Posts:
    3
    @NotSoCreative

    How did you fix it? I'm encountering the same problem as well.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    Again I suggest,

    How to fix a NullReferenceException in Unity3D:

    http://plbm.com/?p=221
     
    Joe-Censored likes this.
  8. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Could add to that page an example of how to check what is null on a line with multiple potential null references, and that Debug.Log can be helpful. (you did ask for comments up in comment #3 :p )

    Just as an example:
    Code (csharp):
    1. GameObject.FindWithTag("Player").GetComponent<CharacterStats>().AddToStrength(5);  //null ref error here, I needz helpz!!!
    Comment out original, and rewrite broken up as several lines, and add checks for anything which can be null:
    Code (csharp):
    1. //GameObject.FindWithTag("Player").GetComponent<CharacterStats>().AddToStrength(5);  //null ref error here, I needz helpz!!!
    2. GameObject playerObject = GameObject.FindWithTag("Player");
    3. if (playerObject == null)
    4. {
    5.     Debug.Log("FindWithTag returned null for Player tag");
    6. }
    7. CharacterStats stats = playerObject.GetComponent<CharacterStats>();
    8. if (stats == null)
    9. {
    10.     Debug.Log("playerObject.GetComponent<CharacterStats> returned null");
    11. }
    12. stats.AddToStrength(5);
     
    Last edited: May 28, 2020
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    I like it. Thanks Uncle Joe. Might just add your exact example, if you don't mind.
     
    Joe-Censored likes this.
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    @Joe-Censored Thanks again... updated the original with a link to this new separate post:

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

    Wrestled a bit with the WordPress formatting, which ate selective portions of the copy-pasted code, and I am still not happy with the lack of indentation on the blocks, alas.
     
  11. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Yeah no problem :)
     
    Kurt-Dekker likes this.
  12. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Does wordpress let you put html spaces in for indentation?
    Code (csharp):
    1. &nbsp;&nbsp;&nbsp;&nbsp;This should look indented
     
  13. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    Apparently not. In code blocks it just literally copies the &nbsp right in, which in some ways makes sense.

    But overall I have to observe that the WordPress editing experience has steadily worsened over the past five years with every attempt to improve it. Virtually every single change since I first installed it either constantly triggers inadvertent mis-edits, makes it incredibly difficult to do simple things (join two blocks... come on!), etc.

    Here is a clue to the WordPress people: make it like a "regular" text editor. Then stop. Please.

    I never thought I would long for the day when my website proudly displayed,

    powered_by_vi.png
     
    Joe-Censored likes this.