【问题标题】:How do I fix this Idle-Car Game Script如何修复此空闲汽车游戏脚本
【发布时间】:2018-10-06 02:18:00
【问题描述】:

我正在尝试制作一款每个人都可以购买汽车的游戏(我将这些数据保存到玩家偏好设置)。所以我的游戏中有 9 条汽车轨迹,我正在尝试编写一些代码,这样当你按下按钮时,汽车和该汽车的轨迹就会显示出来。

当点击旁边的按钮时,它会保存该数据,因此当人们重新开始游戏时,他们仍然会打开汽车和小径,无需再次按下按钮。

这是我的代码:

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

public class GameManager : MonoBehaviour
{ 
    public Button[] TrailLevel; 
    public GameObject[] Cars, Trails;
    public Text text; 
    public int CurrentCarToSpawn = 0; 

    private void Start()
    { }

    private void FixedUpdate()
    {
        UpdateCar();
    }

    public void InstantiateCar()
    {
        TrailLevel[CurrentCarToSpawn].gameObject.active = false;
        MineLevel[CurrentCarToSpawn+1].interactable = true;
        PlayerPrefs.SetInt("TrailCountA", PlayerPrefs.GetInt("TrailCountA") + 1);
        PlayerPrefs.Save();
        CurrentCarToSpawn++;
        UpdateCar();
    }

    void UpdateCar()
    {
        int TrailCountA= PlayerPrefs.GetInt("TrailCountA", 1);
        for (int i = 0; i < TrailLevel.Length; i++)
        {
            if (i + 1 > TrailCountA)
            {
                 TrailLevel.interactable = false;
            }
            if (TrailLevel.interactable)
            {
                Trains[CurrentCarToSpawn].gameObject.active = true;
                Mines[CurrentCarToSpawn].gameObject.active = true;
            }
        }
        text.text = PlayerPrefs.GetInt("TrailCountA").ToString();
    }
}

【问题讨论】:

  • 我建议进行编辑以改进代码格式。 UpdateCar() 函数中的一行看起来很奇怪。所以我将 (TrailLevel.interactable) 更改为 if (TrailLevel.interactable)。如果更改与您的源代码不匹配,请撤消更改。
  • 只是复制粘贴的错误,统一就好了,
  • 我同意@Hyarus 修复你的代码格式,如果你这样做,得到答案会容易得多。
  • @Eddge 我修好了,请帮我弄清楚 :)

标签: c# unity3d


【解决方案1】:

从您的代码中可以看出,这是我的处理方式:

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

public class GameManager : MonoBehaviour
{ 
    public Button[] TrailLevel; 
    public GameObject[] Cars, Trails;
    public Text text; 
    public int CurrentCarToSpawn = 0; 

    private void Start()
    { 
        // Load the current car. ADDED
        CurrentCarToSpawn  = PlayerPrefs.getInt("savedSelection", 0);
        // Since we are loading the last selection, we need to call our
        // instantiation method so it can activate the appropriate 
        // GameObjects.
        InstantiateCar();
    }

    private void FixedUpdate()
    {
        UpdateCar();
    }

    public void InstantiateCar()
    {
        TrailLevel[CurrentCarToSpawn].gameObject.active = false;
        MineLevel[CurrentCarToSpawn+1].interactable = true;
        PlayerPrefs.SetInt("TrailCountA", PlayerPrefs.GetInt("TrailCountA") + 1);
        // Save that this is our current selection.
        PlayerPrefs.SetInt("savedSelection", CurrentCarToSpawn);             
        PlayerPrefs.Save();
        CurrentCarToSpawn++;
        UpdateCar();
    }

    void UpdateCar()
    {
        int TrailCountA= PlayerPrefs.GetInt("TrailCountA", 1);
        for (int i = 0; i < TrailLevel.Length; i++)
        {
            if (i + 1 > TrailCountA)
            {
                 TrailLevel.interactable = false;
            }
            if (TrailLevel.interactable)
            {
                Trains[CurrentCarToSpawn].gameObject.active = true;
                Mines[CurrentCarToSpawn].gameObject.active = true;
            }
        }
        text.text = PlayerPrefs.GetInt("TrailCountA").ToString();
    }
}

【讨论】:

  • 我输入了这段代码,不起作用,按钮都是可交互的,当你按下它们时,它们什么也不做。请帮忙! :)
  • 如果它们都是可交互的,那是因为你没有停用它们,如果它们在你点击它们时什么都不做,那是因为你没有给它们点击操作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多