- Home /
Components with same script destroyed when one of then is destroyed
sorry for english,..... i am working on a horror game i add multiple doors an keys the keys contain same script for multiple doors when i click button on a key all of them is destryoyed...
keyscript... using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Key : MonoBehaviour {
public Door myDoor;
public void UnlockDoor()
{
myDoor.isLocked = false;
Destroy (gameObject);
}
}
Door script.. using System.Collections.Generic; using UnityEngine;
public class Door : MonoBehaviour {
public bool open = false;
private bool hasOpenedCompletely;
public float doorOpenAngle = 90f;
public float doorClosedAngle = 0f;
public float smooth = 2f;
public bool isLocked = false;
public void changeDoorState()
{
if (isLocked != true) {
open = !open;
}
}
// Update is called once per frame
void Update () {
if (open) {
Quaternion targetRotationOpen = Quaternion.Euler (270, doorOpenAngle, 0);
transform.localRotation = Quaternion.Slerp (transform.localRotation, targetRotationOpen, smooth * Time.deltaTime);
} else {
Quaternion targetRotationClosed = Quaternion.Euler (270, doorClosedAngle, 0);
transform.localRotation = Quaternion.Slerp (transform.localRotation, targetRotationClosed, smooth * Time.deltaTime);
}
}
}
interact Script..
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Interact : MonoBehaviour {
public string interactButton;
public float interactDistance = 3f;
public LayerMask interactLayer;
public Image interaction;
public bool isInteracting;
public Button Ibutton;
public Button Dbutton;
void Start () {
if (interaction != null) {
interaction.enabled = false;
Ibutton.gameObject.SetActive (false);
Dbutton.gameObject.SetActive (false);
}
}
// Update is called once per frame2
void Update () {
Ray ray = new Ray (transform.position, transform.forward);
RaycastHit hit;
if (Physics.Raycast (ray, out hit, interactDistance, interactLayer)) {
if (!isInteracting == false) {
if (interaction != null) {
interaction.enabled = true;
}
if (Input.GetButtonDown (interactButton)) {
if (hit.collider.CompareTag ("Key")) {
hit.collider.GetComponent<Key> ().UnlockDoor ();
Ibutton.gameObject.SetActive (true);
} else if (hit.collider.CompareTag ("Door")) {
hit.collider.GetComponent<Door> ().changeDoorState ();
Dbutton.gameObject.SetActive (true);
}
}
}
}
else {
interaction.enabled = false;
}
}
}
Answer by Bunny83 · May 15, 2017 at 07:43 AM
That is not possible unless you attached all those scripts to the same gameobject.
It's not entirely clear which script is attached to which objects. I guess that "Interact" is on the player / camera (depending on the type of game), "Key" is on several gameobjects with a collider placed in the scene.
Physics.Raycast can only return one reference to one object. You call GetComponent to get the reference to the Key instance on that one object. "UnlockDoor" wil destroy that one gameobject that the Key script is attached to. So it's impossible that other objects are destroyed as well unless they are child objects of the destroyed object.
Apart from that you have some weird conditions like:
if (!isInteracting == false) {
this is the same as
if (isInteracting) {
Haha glad you said something, I was staring at the code like... no.. no... that just wouldn't be happening. I was starting to grasp at straws, like trying to conceive of a way that one of these scripts was a prefab lol. Defeinitely something else going on here. The child object sounds like the most logical outcome.
is there any way to replace interact button with touch button so that i can use it for android......any help