- Home /
Deactivating one object trough script deletes all versions of it in scene
I'm fairly new to Unity and I'm having what sounds like a simple problem, but I can't solve it. Basically, I have a scene with a player and two lighthouses, with a small dor in the bottom. The player is supposed to enter the lighthouse and input a password when the correct one is entered the lighthouse gets disabled. The problem is that all lighthouses in the scene get disabled as well.
Here's the overview of my setup: Imgur
And here's the code that deletes it:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BombTrigger : MonoBehaviour {
public GameObject canvas;
public string password = "1234";
public Text text;
void Update () {
//If the text in the UI textbox is the same as the variable "password", then execute the Disable function.
if(text.text == password) {
Disable();
}
}
private void OnTriggerEnter(Collider other) {
//Shows the UI Keypad if the player touches the box object this script is attached to.
if(other.transform.tag == "Player") {
canvas.SetActive(true);
}
}
public void Disable () {
//Hides the UI and (attempts to) deactivate the root object, in this case being the "Lighthouse #" object.
canvas.SetActive(false);
transform.root.gameObject.SetActive(false);
}
}
The lighthouse model is a C4D file imported and placed directly into the scene, and the second Lighthouse is a copy from the one that already was on the scene. I have already tried to create a variable that I can manually set to the Lighthouse object root and then tried to place it in the code instead of transform.root
, but that didn't do anything. Can someone help me? I hope this was well explained and that it isn't as stupid as I think it is. Thanks in advance!
Answer by mani3307 · Mar 24, 2018 at 11:07 AM
@AndroidWG I found out the problem, I think you are referring to the same text component from both the bomb trigger scripts and according to your code in both lighthouses scripts they are continuosly checking the text with the password string and as the text entered matches the password both lighthouses disappear.
Workaround 1.)Create two separate text components, one for each lighthouse and refer them seperately
@mani3307 Thanks for the answer, now I see what's causing the problem, but the password string is on the trigger script attached to the lighthouse, so I just needed to make the passwords different on the Inspector. Thanks a lot man!
Feeling Happy this is my first official answer if you have any doubts please ping me with "@mani3307" I will try to help @AndroidWG