简体   繁体   中英

Create 2 separate instances of the same prefab

I'm trying to create 2 separate instances of the keypad prefab below. The problem is that it doesn't matter on which one of them I tap, they both respond and show the same output. My guess is that they both refer to the same script attached to the prefab and I don't know how to create 2 separate instances of the same script.

键盘图片

Here the script attached to them:

using UnityEngine;
using UnityEngine.UI;

public class LockSystemScript : MonoBehaviour
{

    public Text ScreenText;

    private int _taps;
    private Color _startColor;
    private AudioSource _clickSound;
    private RequestsManager _requestsManager;
    private Vector3 _screenCenter;

    private void Start ()
    {
        _taps = 0;
        _startColor = ScreenText.color;
        _clickSound = GetComponent<AudioSource>();
        _requestsManager = RequestsManager.Instance;
        _screenCenter = GetScreenCenter();
    }

    private void Update () {

        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(_screenCenter);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, 100) && hit.transform.gameObject.tag.Equals("Button") && _taps <= 3)
            {
                ScreenText.text += hit.transform.name;
                _clickSound.Play();
                _taps++;
            }

            if (hit.transform.gameObject.tag.Equals("Submit"))
            {
                _clickSound.Play();
                StartCoroutine(_requestsManager.InputCode(ScreenText.text, ScreenText));
            }

            if (hit.transform.gameObject.tag.Equals("Reset"))
            {
                _taps = 0;
                ScreenText.color = _startColor;
                ScreenText.text = "";
                _clickSound.Play();
            }
        }
    }

    private Vector3 GetScreenCenter()
    {
        float screenX = Screen.width / 2;
        float screenY = Screen.height / 2;
        return new Vector3(screenX, screenY, 0);
    }
}

You do actually have two separate instances of LockSystem and with each of them a separate instance of LockSystemScript . You made a mistake is in the following line though:

if (Physics.Raycast(ray, out hit, 100) && hit.transform.gameObject.tag.Equals("Button") && _taps <= 3)

What you're doing here is you're checking if the Raycast() hits an object and if this object is a Button . The problem is that Update() is being called on every MonoBehaviour in your scene, so it is called for both instances of LockSystemScript and no matter what LockSystem you are looking at, you are looking at a Button and the above statement is true .

What you should be doing is the following:

if (Physics.Raycast(ray, out hit, 100) && hit.transform.gameObject.tag.Equals("Button") && _taps <= 3 && hit.transform.isChildOf(this.transform))

This way you're checking if the Button you are looking at actually belongs to the script in which Update() is run right now.

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