如何解决无法正确使用Unity中其他脚本中的变量
我有以下问题,所以我不能使用其他脚本的可变形式。在我的游戏中,有一个困难的选择屏幕,当用户选择它的级别时,应该给变量适当的值。脚本如下:
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)
编辑:
如评论所示,
问题是两个场景在不同的场景上,因此GameObject.Find
和public GameObject
将给出null
要解决此问题,请使用PlayerPrefs
添加dif = 1.05f
而不是PlayerPrefs.SetFloat("dif",1.05f);
适用于所有dif
个州
而不是difficultMultiplier = d.dif;
添加difficultMultiplier = PlayerPrefs.GetFloat("dif");
并删除“ d”
,我尝试过您的代码看起来可以正常工作。
假设游戏运行正常,并且在播放时获得“空引用”。
您通过检查是否
来签入代码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()来检查返回的值。
其他查询,用于为同一场景传递不同的值
假设您要基于难度级别加载相同的场景,而只是想获得不同的 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");
}
}
一旦我们具有从场景中返回值的功能,接下来我们将对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),maxYspeed) * (Random.value > 0.5f ? -1 : 1));
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。