【问题标题】:value inside switch wont change, unity开关内的值不会改变,统一
【发布时间】:2022-01-07 10:03:27
【问题描述】:

我有开关和一些案例。此代码仅适用于第一行。它需要重新加载两次,以便其余代码可以工作。它一开始会改变敌人的伤害,但需要重新加载两次才能改变killScore。有人知道为什么吗?

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


public class Difficulty : MonoBehaviour
{
    public int difficulty;
    public ammo enemyDamage;
    public enemy enemy1;
    public enemy enemy2;
    //public enemy enemy2;
    public TextMeshProUGUI diff;
    // Start is called before the first frame update
    void Awake()
    {
        difficulty = PlayerPrefs.GetInt("Difficulty");
        diff.text = PlayerPrefs.GetInt("Difficulty").ToString();
    
        switch(PlayerPrefs.GetInt("Difficulty"))
        {
            case 0:
                difficulties(3,100);
                break;
            case 1:
                difficulties(5,200);
                break;
            case 2:
                difficulties(7,300);
                break;
        }
        
        Debug.Log("kill score : " + enemy1.killScore);
        Debug.Log("difficulty : " + difficulty);

    }

    // Update is called once per frame
    void Update()
    {
    }

    void difficulties(int a, int b)
    {
        enemyDamage.enemyDamage = a;
        enemy1.killScore = b;
        enemy2.killScore = b;
    }
}

它改变了预制件上的值,但我放在场景中的预制件没有。

【问题讨论】:

  • 可能是因为您在分配值之前记录了击杀分数?
  • 取出日志后,问题依旧。
  • 到底是什么问题?而且您不应该取出日志,而是在已经分配值后在切换后喜欢它
  • 哦,对不起,先生,我把日志放在第一位的错误。但是在我将日志放在唤醒的最后一部分之后,日志发生了变化。在我添加脚本的项目选项卡上的预制件中,值本身正在发生变化,但是当我更改案例时,我放入场景中的另一个预制件根本没有变化。它需要第二次重新加载才能更改场景中预制件中的值。
  • 如果您更改 prefab 上的值(表示 ProjectView 中的资产),那么它不会对当前 Scene 中的实例产生任何影响。 . 你确定你引用了正确的对象吗?

标签: c# unity3d switch-statement


【解决方案1】:

我更改脚本并将 killScore 放在 PlayerPref 上,它会在播放场景之前在主菜单中更改。谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-23
    • 2022-07-04
    • 1970-01-01
    • 2019-12-28
    • 2018-08-08
    • 2022-01-12
    • 1970-01-01
    • 2019-12-20
    相关资源
    最近更新 更多