Reputation: 5
I have following problem, so i can't use variable form other script. In my game there is difficulty select screen, and when user choose it's level it should give proper value to variable. Script looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class DifManager : MonoBehaviour
{
public float dif;
public void easy()
{
dif = 1.05f;
SceneManager.LoadScene("matchlenght");
}
public void normal()
{
dif = 1.15f;
SceneManager.LoadScene("matchlenght");
}
public void hard()
{
dif = 1.4f;
SceneManager.LoadScene("matchlenght");
}
}
In another script on the gameplay scene i want to use dif variable to set DifficultyMultiplier and it looks like it:
using UnityEngine;
public class Ball2 : MonoBehaviour
{
private matchlength mlength;
public float difficultMultiplier;
public float minXspeed = 0.8f;
public float maxXspeed = 1.2f;
public float minYspeed = 0.8f;
public float maxYspeed = 1.2f;
public DifManager d;
private Rigidbody2D ballRigidbody;
private GameObject secondPaddle;
// Start is called before the first frame update
void Start()
{
d = gameObject.GetComponent<DifManager>();
secondPaddle = GameObject.Find("Paddle2");
difficultMultiplier = d.dif;
mlength = gameObject.GetComponent<matchlength>();
ballRigidbody = GetComponent<Rigidbody2D>();
ballRigidbody.velocity = new Vector2(Random.Range(minXspeed, maxXspeed) * (Random.value > 0.5f ?
-1 : 1), Random.Range(minYspeed, maxYspeed) * (Random.value > 0.5f ? -1 : 1));
}
I'm still getting error about Null reference, so i suppose that is something wrong with getting proper value. What am i doing wrong? Please help me.
Upvotes: 0
Views: 483
Reputation: 640
I have copied these two classes and everything works fine, so the issue caused by another variable, or,
are these two classes inside the same game object? this is the only way I have found to cause this exception... if this is it,
use this line to get "DifManager"
d = GameObject.Find("yourGameObject").GetComponent<DifManager>();
or
Public GameObject s // the game object have "DifManager" class
d = s.GetComponent<DifManager>();
if not,
try debugging your code...
and see where the exception refers to?
like Ball2.Start () (at Assets/Scripts/Ball2.cs:23)
Edit:
as shown in comments,
the issue is the 2 classes on different scenes, so GameObject.Find
and public GameObject
will give null
to solve this issue, Use PlayerPrefs
instead of dif = 1.05f
add PlayerPrefs.SetFloat("dif", 1.05f);
for all dif
states
and instead of difficultMultiplier = d.dif;
add difficultMultiplier = PlayerPrefs.GetFloat("dif");
and remove "d"
Upvotes: 1
Reputation: 346
I tried your code looks to be working fine.
Assuming the game is working fine and you are getting Null Reference when you hit the play.
You check in your code by checking whether the
d = gameObject.GetComponent();
is being referenced or not.
void Start()
{
d = gameObject.GetComponent<DifManager>();
secondPaddle = GameObject.Find("Paddle2");
if (d!= null)// Checks if the object exist, based on this you can try debugging.
// you can add the same for SecondPaddle and mlength and check if its not equals to null....
{
difficultMultiplier = d.dif;
}
mlength = gameObject.GetComponent<matchlength>();
ballRigidbody = GetComponent<Rigidbody2D>();
ballRigidbody.velocity = new Vector2(Random.Range(minXspeed, maxXspeed) * (Random.value > 0.5f ?
-1 : 1), Random.Range(minYspeed, maxYspeed) * (Random.value > 0.5f ? -1 : 1));
}
Also try adding Debug.Log() to check what value is being returned.
Additional Query For passing different values for the Same Scene
Assuming that you want to load the same Scene based upon the difficulty level and just want to get the different dif value.
Please find the below code changes that is required.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class DifManager : MonoBehaviour
{
public static float dif=0.0f; // change value as static
public static float GetdiffVariable() { return dif; }//We will take this function to return us the "dif" value based upon the Scene in our case its easy,normal or hard
public void easy()
{
dif = 1.05f;
SceneManager.LoadScene("matchlenght");
}
public void normal()
{
dif = 1.15f;
SceneManager.LoadScene("matchlenght");
}
public void hard()
{
dif = 1.4f;
SceneManager.LoadScene("matchlenght");
}
}
Once we had the function to return our value from our scene next we make changes to Ball2 Script
using UnityEngine;
public class Ball2 : MonoBehaviour
{
private matchlength mlength;
public float difficultMultiplier;
public float minXspeed = 0.8f;
public float maxXspeed = 1.2f;
public float minYspeed = 0.8f;
public float maxYspeed = 1.2f;
public DifManager d;
private Rigidbody2D ballRigidbody;
private GameObject secondPaddle;
// Start is called before the first frame update
void Start()
{
//d = gameObject.GetComponent<DifManager>(); we dont need this line anymore we can directly assign as we are only concerned with the return value for our Scene Difficulty
difficultMultiplier = d.GetdiffVariable();//Should return you different values based on the easy, normal or hard.
Debug.Log("difficultMultiplier");// cross verify in console
secondPaddle = GameObject.Find("Paddle2");
mlength = gameObject.GetComponent<matchlength>();
ballRigidbody = GetComponent<Rigidbody2D>();
ballRigidbody.velocity = new Vector2(Random.Range(minXspeed, maxXspeed) * (Random.value > 0.5f ?
-1 : 1), Random.Range(minYspeed, maxYspeed) * (Random.value > 0.5f ? -1 : 1));
}
Upvotes: 0