无法在 Unity 中正确使用来自其他脚本的变量
Can't properly use variable from other script in Unity
我有以下问题,所以我不能使用其他脚本的变量形式。在我的游戏中有难度 select 屏幕,当用户选择它的级别时,它应该给变量适当的值。脚本如下所示:
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");
}
}
在游戏场景的另一个脚本中,我想使用 dif 变量来设置 DifficultyMultiplier,它看起来像这样:
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));
}
我仍然收到关于 Null 引用的错误,所以我想获取正确的值是有问题的。我究竟做错了什么?请帮助我。
我已经复制了这两个类并且一切正常,所以问题是由另一个变量引起的,
或者,
这两个 类 是同一个游戏对象吗?这是我发现导致此异常的唯一方法......
如果是这样,
使用此行获取“DifManager”
d = GameObject.Find("yourGameObject").GetComponent<DifManager>();
或
Public GameObject s // the game object have "DifManager" class
d = s.GetComponent<DifManager>();
如果没有,
尝试调试您的代码...
看看异常指的是哪里?
喜欢Ball2.Start () (at Assets/Scripts/Ball2.cs:23)
编辑:
如评论所示,
问题是不同场景下的 2 类,所以 GameObject.Find
和 public GameObject
会给出 null
解决这个问题,使用PlayerPrefs
而不是 dif = 1.05f
添加 PlayerPrefs.SetFloat("dif", 1.05f);
对于所有 dif
个州
而不是 difficultMultiplier = d.dif;
添加 difficultMultiplier = PlayerPrefs.GetFloat("dif");
并删除“d”
我试过你的代码看起来工作正常。
假设游戏运行良好,并且您在玩游戏时得到的是 Null Reference。
您通过检查
d = gameObject.GetComponent();
是否被引用。
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));
}
也可以尝试添加 Debug.Log() 来检查正在 returned 的值。
为同一场景传递不同值的附加查询
假设您想要根据难度级别加载相同的场景,并且只想获得不同的 dif 值。
请找到以下所需的代码更改。
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");
}
}
一旦我们有了 return 我们场景中的值的功能,接下来我们将更改 Ball2 脚本
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));
}
我有以下问题,所以我不能使用其他脚本的变量形式。在我的游戏中有难度 select 屏幕,当用户选择它的级别时,它应该给变量适当的值。脚本如下所示:
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");
}
}
在游戏场景的另一个脚本中,我想使用 dif 变量来设置 DifficultyMultiplier,它看起来像这样:
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));
}
我仍然收到关于 Null 引用的错误,所以我想获取正确的值是有问题的。我究竟做错了什么?请帮助我。
我已经复制了这两个类并且一切正常,所以问题是由另一个变量引起的, 或者,
这两个 类 是同一个游戏对象吗?这是我发现导致此异常的唯一方法...... 如果是这样,
使用此行获取“DifManager”
d = GameObject.Find("yourGameObject").GetComponent<DifManager>();
或
Public GameObject s // the game object have "DifManager" class
d = s.GetComponent<DifManager>();
如果没有,
尝试调试您的代码...
看看异常指的是哪里?
喜欢Ball2.Start () (at Assets/Scripts/Ball2.cs:23)
编辑:
如评论所示,
问题是不同场景下的 2 类,所以 GameObject.Find
和 public GameObject
会给出 null
解决这个问题,使用PlayerPrefs
而不是 dif = 1.05f
添加 PlayerPrefs.SetFloat("dif", 1.05f);
对于所有 dif
个州
而不是 difficultMultiplier = d.dif;
添加 difficultMultiplier = PlayerPrefs.GetFloat("dif");
并删除“d”
我试过你的代码看起来工作正常。
假设游戏运行良好,并且您在玩游戏时得到的是 Null Reference。
您通过检查
d = gameObject.GetComponent();
是否被引用。
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));
}
也可以尝试添加 Debug.Log() 来检查正在 returned 的值。
为同一场景传递不同值的附加查询
假设您想要根据难度级别加载相同的场景,并且只想获得不同的 dif 值。
请找到以下所需的代码更改。
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");
}
}
一旦我们有了 return 我们场景中的值的功能,接下来我们将更改 Ball2 脚本
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));
}