0

I am trying to learn Unity by making a simple clicker game. I have made a clicker to increase your click count which works fine and have tried to make a click power upgrade which does not work.

I am also new to C sharp so I have stored all of my variables in another script called Variables.cs:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Variables : MonoBehaviour
{
    public int Clicks;
    public int ClickPower;
    public int ClickPowerUpgradeCost;
}

This is the code in UpgradeClickPower.cs:

using System.Collections.Generic;
using UnityEngine;

public class UpgradeClickPower : MonoBehaviour
{
    public Variables vars;
    
    public void Upgrade()
    {
        vars.ClickPower += 1;
        vars.Clicks -= vars.ClickPowerUpgradeCost;
    }
}

And this is the error message I receive whenever I try and use the upgrade button:

NullReferenceException: Object reference not set to an instance of an object

I am aware this means something I'm trying to use is returning a null value but I don't know what it could be since everything in variables.cs has a value.

Also I am very new to this so if you have anything to say about my poor coding/understanding please feel free to tell me what I'm doing wrong/ineffectively.

  • `Variables` is a class (i.e. a reference type). Fields and autoproperties for storing reference types are `null` by default. – Llama Aug 12 '20 at 09:31
  • isn't it `public Variables vars = new Variables();? The instantiation – Prashanth Benny Aug 12 '20 at 09:32
  • What do I do about it? Sorry if this is really simple but I have only just started getting into unity and C sharp yesterday. –  Aug 12 '20 at 09:33
  • you should add a constructor wih parameters to your `Variables` class and then in`UpgradeClickPower` you should create a `new Variables` with that constructor you created. Otherwise the `Variables` properties will always be null – Rubs Bieira Aug 12 '20 at 09:37
  • have a look at [class constructors](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/constructors) – Cleptus Aug 12 '20 at 09:44
  • This is not a duplicate, eventhough the error is the same. Rubs is incorrect, you do not use constructors in Unity. – TJHeuvel Aug 12 '20 at 10:59
  • In this case; i wouldnt use another script for Variables. Just define those variables inside the first script. – TJHeuvel Aug 12 '20 at 10:59
  • 1
    Maybe you should forget about Unity for few weeks and concentrate only on C#. Both Unity and programing are really overwhelming (in the beginning) on their own. Starting both simultaneously can be an extreme challenge. Go with C# first because without basic programing Unity won't make any sense. – Nikaas Aug 12 '20 at 11:05
  • 1
    Yeah, I only have experience in Python so I'm probably going to take your advice and only do C sharp for now. Also I'm not an idiot, my keyboard is on the US version and I can't be bothered to change it back lol. Thanks! –  Aug 12 '20 at 13:42

0 Answers0