Variables 无法在Unity中正确使用来自其他脚本的变量

Variables 无法在Unity中正确使用来自其他脚本的变量,variables,scripting,Variables,Scripting,我有以下问题,所以我不能使用变量形式的其他脚本。在我的游戏中,很难选择屏幕,当用户选择它的级别时,它应该给变量适当的值。脚本如下所示: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; public class DifManager : MonoBehaviour { pub

我有以下问题,所以我不能使用变量形式的其他脚本。在我的游戏中,很难选择屏幕,当用户选择它的级别时,它应该给变量适当的值。脚本如下所示:

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变量设置困难乘法器,它看起来是这样的:

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));
    
}

我仍然得到关于空引用的错误,所以我想这是获取正确值的错误。我做错了什么?请帮帮我。

我已经复制了这两个类,并且一切正常,因此问题是由另一个变量引起的, 或者

这两个类在同一个游戏对象中吗?这是我发现的唯一导致此异常的方法。。。 如果是这样,

使用此行获取DifManager

d =  GameObject.Find("yourGameObject").GetComponent<DifManager>();

如果没有,

尝试调试您的代码

看看例外指的是什么

比如Ball2。从Assets/Scripts/Ball2开始。cs:23

编辑:

如评论所示, 问题是这两个类在不同的场景中,所以GameObject.Find和PublicGameObject将给出null

要解决此问题,请使用PlayerPrefs

添加PlayerPrefs.SetFloatdif,1.05f,而不是dif=1.05f; 适用于所有dif状态

而不是困难的乘法器=d.dif;添加困难乘数=PlayerPrefs.GetFloatdif


并删除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));

}
一旦我们有了从场景中返回值的函数,接下来我们将对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));
    
}

谢谢你的回答,现在无论我选择哪种难度等级,我都得到了困难倍数值1.1f。我不知道怎么修理它。也许你能再帮我一次吗?嗨,我试过这一次,它一点变化也没有。但我知道dif总是有1.1的值。它在unity编辑器中分配给游戏对象控制器,并附加了脚本DiffManager,但我不知道如何更改它,以获得我想要的不同变量。@Pjoter DiffManger您的场景管理器对吗?这将是不同场景的一部分,我假设,不要在游戏场景中附加场景管理器,你需要从Diff Manger调用函数到你当前拥有游戏对象的场景。谢谢你的帮助,我已经尝试过了,但没有任何变化。请解释发生了什么,首先,这两个类在同一个游戏对象中吗?如果没有,请确保您已正确设置游戏对象名称谢谢您的时间和建议,伙计。所以我有3个场景:第一个场景,你选择难度,有一个游戏对象,它是我的控制器,它叫做难度管理器,这个游戏对象附带了脚本DifManager,你可以在上面看到。第二个场景是你们选择你们或对手在游戏结束时必须得多少分并检查谁赢。第三个场景是游戏场景,有4个游戏对象-球、两个桨和游戏控制器。脚本Ball2.cs附加到ball gameobject,您可以在上面查看该脚本。游戏对象名称应该是正确的,因为我已经复制了它的名称。现在我得到了它,我的脚本结果将设置d为“null”,因为它试图在另一个场景中找到一个对象,尝试使用PlayerRefs,在DifManager上设置dif,在Ball2上获得dif。你可以更清楚地写下你使用PlayerRefs的意思吗。对不起,我不明白;。
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");
}
}
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));
    
}