I know this question asked before but it’s bit different i think.
Let me introduce the problem with steps.
1-) I am trying to make simple clicker game for android.
2-) So i have class called “playerData.cs” and variables on it.
3-) I am trying to acces a variable of “playerData.cs” class.
4-) In other script i already made definations for this.
5-) I have used the same thing in my older projects and it was working.
6-) Magicly , it’s not working for this project.
Let me add some code below…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; // added for access ui elements via code
public class playerData : MonoBehaviour {
// handles the player's data for game
#region public variables
public Slider progressBar; // attach it from inspector
public Text percantageText; // attach it from inspector
public Text waterDropsCount; // attach it from inspector
public float playerProgress; // min. value = 1 , max value = 100
// collected elements
public int waterDrops = 0;
#endregion
void Start() {
// init. player's data
progressBar.value = playerProgress;
percantageText.text = playerProgress.ToString () + "%";
waterDropsCount.text = "x" + waterDrops.ToString ();
}
}
So what this script does is : Gets the player’s data on start of scene and attach the variables to ui objects.
My other script that i am creating a instance of this class :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class waterDrop : clickables {
#region private variables
private playerData instance; // we need this here
#endregion
void Start() {
instance = gameObject.AddComponent<playerData> (); // init. the variable
}
public void clickWaterDrop() {
// simply gain water drops when you click it
// plus value is inherited from "clickables.cs" base script
instance.waterDrops += plusValue;
}
}
İ added this script to the click able game object.Simply when you click or tab this object you gain things.
Error pops up in the “playerData.cs” script line 25 which is (progressBar.value = playerProgress;) part.
Error text : Object reference not set to an instance of an object.
Thank you !