You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity项目中C# int变量无法覆盖的问题求助

问题根源

你遇到的核心问题是**tempChernoff变量属于单个MapButtons实例**:你点击的Chernoff按钮和国家按钮是挂载了MapButtons脚本的不同对象——也就是说,你在Chernoff按钮实例里修改的tempChernoff,和国家按钮实例里读取的tempChernoff根本不是同一个变量,后者自然还是初始值0。

解决方案

下面是几种可行的修复方式,按推荐程度排序:

1. 使用静态变量共享状态

把tempChernoff声明为静态变量,让所有MapButtons实例共享同一个值:

// 替换原来的public int tempChernoff = 0;
public static int tempChernoff = 0;

注意:静态变量是全局共享的,若需在场景切换时重置状态,记得在合适时机(比如SceneManager.sceneLoaded事件中)将其重置为0。

2. 实现单例模式管理状态

创建专门的单例类统一管理Chernoff选择状态,避免静态变量的全局污染:

public class ChernoffManager : MonoBehaviour
{
    // 全局唯一实例
    public static ChernoffManager Instance { get; private set; }

    public int SelectedChernoffId { get; set; } = 0;

    private void Awake()
    {
        // 确保场景中只有一个实例
        if (Instance != null && Instance != this)
        {
            Destroy(this.gameObject);
        }
        else
        {
            Instance = this;
            DontDestroyOnLoad(this.gameObject);
        }
    }
}

然后修改MapButtons中的逻辑:

// 替换原selectChernoff方法
public void selectChernoff()
{
    switch (currentButton.name)
    {
        case "Chernoff1":
            ChernoffManager.Instance.SelectedChernoffId = 1;
            this.gameObject.GetComponent<Image>().color = new Color(102, 255, 51);
            Debug.Log(ChernoffManager.Instance.SelectedChernoffId);
            break;
        case "Chernoff2":
            ChernoffManager.Instance.SelectedChernoffId = 2;
            this.gameObject.GetComponent<Image>().color = new Color(102, 255, 51);
            Debug.Log(ChernoffManager.Instance.SelectedChernoffId);
            break;
    }
}

// 替换原selectCountry方法
public void selectCountry()
{ 
    int selectedId = ChernoffManager.Instance.SelectedChernoffId;
    Debug.Log(selectedId);
    
    switch (selectedId)
    {
        case 1:
            chernoffAvatar = GameObject.Find("Chernoff1");
            chernoffAvatar.GetComponent<Image>().color = new Color(130, 130, 130);
            chernoffAvatar = GameObject.Find("ChernoffAvatar1");
            break;
        case 2:
            chernoffAvatar = GameObject.Find("Chernoff2");
            chernoffAvatar.GetComponent<Image>().color = new Color(130, 130, 130);
            chernoffAvatar = GameObject.Find("ChernoffAvatar2");
            break;
        case 0:
            Debug.Log("No ChernoffAvatar Selected");
            break;
    }

    if(selectedId != 0)
    {
        var currentCountry = chernoffAvatar.transform.Find("Country Text").GetComponent<TextMeshPro>().text;
        Debug.Log(currentCountry);
        Debug.Log(currentButton.name);
    }
}

这种方式更符合Unity最佳实践,状态管理更清晰,也方便后续扩展。

3. 直接引用目标实例

如果场景中只有两个Chernoff按钮,可以在国家按钮的MapButtons脚本里直接引用这两个按钮的组件,读取它们的状态:

// 在国家按钮的MapButtons脚本中添加引用
public MapButtons chernoff1Btn;
public MapButtons chernoff2Btn;

public void selectCountry()
{ 
    int tempChernoff = 0;
    if(chernoff1Btn.tempChernoff == 1)
    {
        tempChernoff = 1;
    }
    else if(chernoff2Btn.tempChernoff == 2)
    {
        tempChernoff = 2;
    }

    Debug.Log(tempChernoff);
    // 后续逻辑与原代码一致
}

需要在Unity编辑器中,将两个Chernoff按钮拖到国家按钮脚本的对应引用槽中,适合简单场景快速解决问题。

额外优化建议
  • 避免频繁调用GameObject.Find和GetComponent,可以在Start方法中提前获取组件引用并存储,提升运行性能。
  • Start方法中的CSV读取逻辑会被每个MapButtons实例重复执行,建议将其移到单例类中,只执行一次。

内容的提问来源于stack exchange,提问作者Palucca

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 16:27:47