简体   繁体   中英

Assign a tag to an object in a unity

I'm making a subway simulator, I want the StationBox to always have the Created tag after pressing a button, but it goes back to the previous value when the game is restarted, how can I solve this?

Update: I will have more than 1 station, I would like to make a universal script for all

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

public class AddStation : MonoBehaviour
{
    public InputField Input;
    public Text StationName;
    public GameObject Button;
    public GameObject CreateStation;
    public GameObject StationBox;

    public void OnMouseDown(){
        if (Input.text != ""){
            CreateStation.SetActive(false);
            StationName.text = Input.text.ToString();
            StationBox.tag = "Created";
        }
    }
}

If I understand correctly, you are trying to find a way to save game data so as not to lose it when you restart the game. To do this the most practical solution is PlayerPrefs. You can find a lot of documentation online because it's a simple concept. However for your problem you can do this:

public void OnMouseDown(){
        if (Input.text != ""){
            CreateStation.SetActive(false);
            StationName.text = Input.text.ToString();
            StationBox.tag = "Created”;
            PlayerPrefs.SetInt(“Created”, 1);
        }
    }

    void Start()
    {
       if (PlayerPrefs.HasKey(“Created”))
         if (PlayerPrefs.GetInt(“Created”) == 1)
             StationBox.tag = "Created”;
    }

I hope I have helped you. If so, you can thank me by marking this answer as accepted :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM