变量不改变交叉类的统一

时间:2017-02-27 18:52:27

标签: c# unity3d unity5

我的脚本需要更改不同类中的变量。我已经设置了所有类引用,变量是公共的,但它仍然不会改变。

这是发送类:

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()
{

}

}

接收班在这里:

    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)。

1 个答案:

答案 0 :(得分:3)

如果我理解正确,你想要改变所有已经生成的Rock类实例的速度,甚至可能改变未来实例的速度。

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

我只会向您展示所需的代码。

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

生成器类(我已将所有非相关代码剪切到解决方案中,只需自行添加)

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

基本上,委托/事件组合将改变所有现有实例的速度,而CreateObstacle方法将创建具有最后选择的速度的实际速度(Actualvel)。

修改:我会进一步详细说明。

1)当然,您需要在资产中安装Rock预制件并附加Rock.cs脚本。

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

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

在CreateObstacle()内部行,并移除Actuavel = rockDelegate()内的OnGUI()

3)如果要仅更改新实例的Rock对象的速度,请更改

rockDelegate(Actualvel = Easyvel);

行到Actuavel = Easyvel,然后删除

Generator.rockDelegate += ChangeVelocity;

来自Rock.cs脚本。您也可以摆脱所有委托/事件行。