'Unity C# Script only works on one object
I have multiple objects with the same script on one scene, however, the script works only for the first object that I instantiated the script with and refuses to work for the rest, has anyone else encountered this problem and how did you solve/troubleshoot it? Note that the class of the script and its variables are not static
the following is the script in question, the most important to note here is the CheckInteract method since it's the first one to be called in the game.
The CheckInteract basically checks whether an object with a collider with the player mask is within the bounds of the CheckSphere, if it is then the player can interact with it. it should work in all instances but it only works on the first instantiated object
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class PasswordTerminal : MonoBehaviour
{
[SerializeField] protected LayerMask playerLayerMask;
[SerializeField] protected float interactRadius;
private bool terminalIsActive = false;
[SerializeField] private GameObject terminalPanel;
private TextMeshProUGUI terminalText;
[SerializeField] private TerminalDoor terminalDoors;
[SerializeField] private int terminalPassLength = 4;
[SerializeField] private string terminalPassword;
private KeyCode[] passwordKeyCodes = {
KeyCode.Alpha0,
KeyCode.Alpha1,
KeyCode.Alpha2,
KeyCode.Alpha3,
KeyCode.Alpha4,
KeyCode.Alpha5,
KeyCode.Alpha6,
KeyCode.Alpha7,
KeyCode.Alpha8,
KeyCode.Alpha9,
};
private void Start()
{
terminalText = terminalPanel.transform.GetComponentInChildren<TextMeshProUGUI>();
}
private void Update()
{
CheckInteract();
ActivateTerminal();
InteractPanel();
}
public void CheckInteract()
{
if (Physics.CheckSphere(transform.position, interactRadius, playerLayerMask))
{
Debug.Log("player is within range");
if (Input.GetKeyDown(KeyCode.E))
{
Interact();
}
}
else
{
Debug.Log("player is not within range");
}
}
private void Interact()
{
terminalIsActive = !terminalIsActive;
}
private void ActivateTerminal()
{
if (terminalIsActive)
{
terminalPanel.SetActive(true);
}
else
{
terminalPanel.SetActive(false);
}
if (!Physics.CheckSphere(transform.position, interactRadius, playerLayerMask))
{
terminalIsActive = false;
}
}
private void InteractPanel()
{
if (terminalIsActive)
{
for (int i = 0; i < passwordKeyCodes.Length; i++)
{
if (Input.GetKeyDown(passwordKeyCodes[i]))
{
if (terminalText.text.Length >= terminalPassLength)
{
terminalText.text = "";
}
terminalText.text += i;
}
}
if (Input.GetKeyDown(KeyCode.Return))
{
if (terminalText.text == terminalPassword)
{
terminalDoors.ActivateDoors();
terminalIsActive = false;
}
terminalText.text = "";
}
}
}
private void OnDrawGizmosSelected()
{
Gizmos.color = new Vector4(0, 255, 0, 0.5f);
Gizmos.DrawSphere(transform.position, interactRadius);
}
}
All annotated objects has the same script, however only one works
Stuff that I've already tried:
- Deleting the first instance, results in not a single object working
- Adding the objects and scripts in another scene, the same thing happens, only the first instance is called
- Restarting unity, the same thing happens
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|