Как мне исправить этот скрипт игры на холостом ходу? - PullRequest
0 голосов
/ 05 октября 2018

Я пытаюсь создать игру, в которой каждый может покупать машины (и я сохраняю эти данные в настройках playerprefs).Итак, у меня в игре 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();
    }
}

1 Ответ

0 голосов
/ 05 октября 2018

Из того, что я вижу в вашем коде, я бы подошел к нему так:

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();
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...