变量不改变 cross class unity

Variable not changing cross class unity

我的脚本需要更改另一个 class 中的变量。我已经设置了所有 class 引用并且变量是 public 但它仍然不会改变。

这是发送class:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Generator : MonoBehaviour {

public GameObject rocks;
public int score;
public int HsR;
Player Script;
GameObject player;
float Difficulty;
Rock rock;
Vector2 Easyvel;
Vector2 Mediumvel;
Vector2 Hardvel;

// Use this for initialization
void Start()
{
    Difficulty = 1.5f;
    InvokeRepeating("CreateObstacle", 1f, Difficulty);
    score = -2;
    player = GameObject.FindGameObjectWithTag("Player");
    Script = player.GetComponent<Player>();
    HsR = PlayerPrefs.GetInt("HighscoreReset");
    rock = rocks.GetComponent<Rock>();
    Easyvel = new Vector2(-4, 0);
    Mediumvel = new Vector2(-8,0);
    Hardvel = new Vector2(-12, 0);
}

void CreateObstacle()
{
    Instantiate(rocks);
    score++;
    if (score > 0)
    {
        Script.Score();
    }
}

void OnGUI()
{
    GUI.color = Color.black;
    GUILayout.Label(" Score: " + score.ToString());
    GUILayout.Label(" Highscore: " + PlayerPrefs.GetInt("Highscore").ToString());
    GUILayout.Label("Highscore Reset: " + HsR);
    if (GUI.Button(new Rect(300, 10, 50, 30), "Reset"))
    {
        PlayerPrefs.SetInt("Highscore", 0);
        HsR++;
        PlayerPrefs.SetInt("HighscoreReset", HsR);
    }

    if (GUI.Button(new Rect(600, 10, 50, 30), "Easy"))
    {
        Difficulty = 1.5f;
        rock.velocitypublic = Easyvel;
    }

    if (GUI.Button(new Rect(600, 40, 50, 30), "Medium"))
    {
        Difficulty = 1f;
        rock.velocitypublic = Mediumvel;
    }

    if (GUI.Button(new Rect(600, 70, 50, 30), "Hard"))
    {
        Difficulty = 0.5f;
        rock.velocitypublic = Hardvel;
        Debug.Log("Hard " + rock.velocitypublic + Difficulty);
    }
}

void Update()
{

}

}

接收class在这里:

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rock : MonoBehaviour {

    public Rigidbody2D rb2d;
    public Vector2 velocitypublic;

    // Use this for initialization
    void Start()
    {
        velocitypublic = new Vector2(-4, 0);
        rb2d = gameObject.GetComponent<Rigidbody2D>();
        transform.position = new Vector3(transform.position.x, transform.position.y - Random.Range(-2f, 2f), transform.position.z);

    }

    void Update()
    {
        rb2d.velocity = velocitypublic;
        Debug.Log(velocitypublic);
    }
}

需要更改的变量是velocitypublic。它应该是 (-12, 0) 在我点击硬按钮之后它仍然是 (-4, 0).

如果我没理解错的话,您想更改所有已生成的 Rock 实例的速度 class,甚至可能更改未来实例的速度。

为了更改 Rock class 的所有实例(现在和未来)的速度,您可以使用带有委托+事件的观察者模式,并更改新 Rock 实例的生成方式。

我只会告诉你需要的代码。

摇滚Class

using UnityEngine;    

public class Rock : MonoBehaviour {

    public Rigidbody2D rb2d;
    public Vector2 velocitypublic;

    void Start() {
        Generator.rockDelegate += ChangeVelocity;
        rb2d = gameObject.GetComponent<Rigidbody2D>();
        transform.position = new Vector3(transform.position.x, transform.position.y - Random.Range(-2f, 2f), transform.position.z);
    }

    void ChangeVelocity(Vector2 vel){
        velocitypublic = vel;
    }
}

GeneratorClass(我把解决方案中的非相关代码都删掉了,自己加就行)

using UnityEngine;

public delegate void RockDelegate(Vector2 vel);

public class Generator : MonoBehaviour {

    public static event RockDelegate rockDelegate;

    public GameObject rocks;
    Vector2 Actualvel, Easyvel, Mediumvel, Hardvel;
    float Difficulty = 1.5f;

    void Start(){
        InvokeRepeating("CreateObstacle", 1f, Difficulty);
        Easyvel = new Vector2(-4, 0);
        Mediumvel = new Vector2(-8,0);
        Hardvel = new Vector2(-12, 0);
        Actualvel = Mediumvel; //<- Here you assign the starting velocity before any Button is pressed
    }

    void CreateObstacle() {
        GameObject rock = Instantiate(rocks) as GameObject;
        rock.GetComponent<Rock>().velocitypublic = Actualvel;
    }

    void OnGUI() {
        if (GUI.Button(new Rect(600, 10, 50, 30), "Easy")) {
            Difficulty = 1.5f;
            rockDelegate(Actualvel = Easyvel);
        }

        if (GUI.Button(new Rect(600, 40, 50, 30), "Medium")) {
            Difficulty = 1f;
            rockDelegate(Actualvel = Mediumvel);
        }

        if (GUI.Button(new Rect(600, 70, 50, 30), "Hard")) {
            Difficulty = 0.5f;
            rockDelegate(Actualvel = Hardvel);
        }
    }
}

基本上,delegate/event 组合将改变所有现有实例的速度,而 CreateObstacle 方法将使用最后选择的速度 (Actualvel) 创建新实例。

编辑:我会进一步详细说明一些事情。

1) 当然,您需要在资产中有一个 Rock 预制件,并附有 Rock.cs 脚本。

2) 如果你只想改变已经实例化的岩石对象的速度,只需去掉 Actualvel,即删除

rock.GetComponent<Rock>().velocitypublic = Actualvel;

CreateObstacle() 内的行并删除 OnGUI()rockDelegate() 内的 Actuavel =

3) 如果你只想改变新实例化的岩石对象的速度,改变

rockDelegate(Actualvel = Easyvel);

行到 Actuavel = Easyvel,并删除

Generator.rockDelegate += ChangeVelocity;

来自 Rock.cs 脚本的行。您也可以去掉所有 delegate/event 行。