Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JIME363 · May 15, 2017 at 01:22 AM · gameobjectscript.destroysame

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;
 
         }
 
     }
 }
 
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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) {
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RobAnthem · May 15, 2017 at 07:46 AM 0
Share

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.

avatar image JIME363 · May 15, 2017 at 07:50 AM 0
Share

is there any way to replace interact button with touch button so that i can use it for android......any help

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

More than one gameObject on same script / The script only works for one gameobject and work on the rest of the gameobjects 1 Answer

Destroy GameObject Variable? 3 Answers

Same Prefab, Same Script, Different Values? 1 Answer

How to only delete one of two collided objects? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges