Search Unity

please help error

Discussion in '2D' started by Karmjotgirn, Nov 5, 2016.

  1. Karmjotgirn

    Karmjotgirn

    Joined:
    Nov 5, 2016
    Posts:
    17
    i get this error when i try playing my game
    NullReferenceException: Object reference not set to an instance of an object
    Click.Update () (at Assets/Scripts/Click.cs:13)
    here is my script it's c#\

    using UnityEngine;
    using System.Collections;

    public class Click : MonoBehaviour {

    public UnityEngine.UI.Text fpc;
    public UnityEngine.UI.Text fatDisplay;
    public float fat = 0.00f;
    public int fatperclick = 1;

    void Update () {
    fatDisplay.text = "Fat: " + fat;
    fpc.text = "FPC: " + fatperclick;
    }

    public void Clicked(){
    fat += fatperclick;
    }

    }
     
  2. albertvasconcellos

    albertvasconcellos

    Joined:
    Feb 18, 2016
    Posts:
    5
    You should double check your fpc and fatDisplay elements in the inspector, they may not be assigned.
     
  3. Karmjotgirn

    Karmjotgirn

    Joined:
    Nov 5, 2016
    Posts:
    17
    what do you mean??
     
  4. Karmjotgirn

    Karmjotgirn

    Joined:
    Nov 5, 2016
    Posts:
    17
    I'm new how do i do that?
     
  5. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    When you define public variables, they show up in the Unity Editor on the component in the Inspector. You need to drag and drop the appropriate objects into the component fields for the component to use them, or click the little circle next to them and choose from the list.

    You have two public Text fields, so your "Click" component will have two "Text" fields that start out empty.
     
  6. Karmjotgirn

    Karmjotgirn

    Joined:
    Nov 5, 2016
    Posts:
    17
    i don't get it :( im sorry if im giving you trouble
     
  7. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    When you add the Click component to an object, you will see this. See those two fields that say "none"? You need to drag into those fields the corresponding UI Text objects that you're trying to use.
     

    Attached Files:

  8. Karmjotgirn

    Karmjotgirn

    Joined:
    Nov 5, 2016
    Posts:
    17
    ok let me try it
     
  9. Karmjotgirn

    Karmjotgirn

    Joined:
    Nov 5, 2016
    Posts:
    17
    here what i get i have no text take a look
     

    Attached Files:

  10. Karmjotgirn

    Karmjotgirn

    Joined:
    Nov 5, 2016
    Posts:
    17
  11. Karmjotgirn

    Karmjotgirn

    Joined:
    Nov 5, 2016
    Posts:
    17
    Assets/Scripts/Click.cs(8,22): error CS0102: The type `Click' already contains a definition for `fat'
    now i got this error
     
  12. Karmjotgirn

    Karmjotgirn

    Joined:
    Nov 5, 2016
    Posts:
    17
    here is my screen now but i got this other error i did what you said
     

    Attached Files:

  13. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Make sure that all the Click components in the scene have their fields assigned correctly.
     
  14. Karmjotgirn

    Karmjotgirn

    Joined:
    Nov 5, 2016
    Posts:
    17
    now i have a erro with line 12 pls halp ;(
     
  15. Karmjotgirn

    Karmjotgirn

    Joined:
    Nov 5, 2016
    Posts:
    17
    i followed this tutorial
     
  16. Karmjotgirn

    Karmjotgirn

    Joined:
    Nov 5, 2016
    Posts:
    17
    if you could point anything i did wrong could you tell me?
     
  17. Karmjotgirn

    Karmjotgirn

    Joined:
    Nov 5, 2016
    Posts:
    17
    and if you have youtube i could subscribe to you
     
  18. Karmjotgirn

    Karmjotgirn

    Joined:
    Nov 5, 2016
    Posts:
    17
    here is my script


    using UnityEngine;
    using System.Collections;

    public class Click : MonoBehaviour {

    public UnityEngine.UI.Text fes;
    public UnityEngine.UI.Text fatDisplay;
    public float fat = 0.00f;
    public int fatperclick = 1;

    void Update () {
    fatDisplay.text = "Fat: " + fat;
    fes.text = "FES: " + fatperclick;
    }

    public void Clicked(){
    fat += fatperclick;
    }

    }
     
  19. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    You need to check that none of your fields say "None". They all need to have objects assigned. That's how to fix this.
     
  20. Karmjotgirn

    Karmjotgirn

    Joined:
    Nov 5, 2016
    Posts:
    17
    i changed some of the names
     
  21. Karmjotgirn

    Karmjotgirn

    Joined:
    Nov 5, 2016
    Posts:
    17
    i did it still problems
     
  22. Karmjotgirn

    Karmjotgirn

    Joined:
    Nov 5, 2016
    Posts:
    17
    using UnityEngine;
    using System.Collections;

    public class Click : MonoBehaviour {

    public UnityEngine.UI.Text fes;
    public UnityEngine.UI.Text fatDisplay;
    public float fat = 0.00f;
    public int fatperclick = 1;

    void Update () {
    fatDisplay.text = "Fat: " + fat;
    fes.text = "FES: " + fatperclick;
    }

    public void Clicked(){
    fat += fatperclick;
    }

    }