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 chainylol · Jul 10, 2017 at 05:16 PM · collisiongameobjectprogrammingmultipledestroygameobject

Colliding with multiple objects to destroy other gameObject

Hey guys,

So i recently made a script for destroying a gameObject (a door) when the player collides and destroys a different gameObject (a key). Basically acting like a key and door system. While this works great, it also heavily limits my level designs since it only requires one collision to get the door destroyed. What i need is a script that requires all keys in a level to be destroyed/collided with in order for the door to get destroyed. I'm thinking that the easiest way to do this is to have a script check if there are any keys left in the scene, and if keys = 0, the door will get destroyed. I'm sure there are other ways too.

Below is the script currently attached to the "key" object.

JS:

var thing : GameObject;

function OnCollisionEnter (col : Collision) {

 if(col.gameObject.tag == "Player")
 {
     Destroy(thing.gameObject);
 }

}

I also have a separate script for destroying the key on collision with the player. That script is attached to the player and looks like this:

using UnityEngine; using System.Collections;

public class DestroyCube : MonoBehaviour { void OnCollisionEnter(Collision col) { if (col.gameObject.name == "Key") { Destroy(col.gameObject); } } }

Any help appreciated. Thanks.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Jwizard93 · Jul 11, 2017 at 01:50 AM

Each key can have a reference to the door. The door has a script with a public int numKeys. then on the keys:

 if(col.gameObject.tag == "Player")
  {
      door.numKeys--;
  }

Then in Update() on the door:

 if (numKeys <= 0)
 {
     Destroy(gameObject);
 }
Comment
Add comment · Show 16 · 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 chainylol · Jul 11, 2017 at 01:53 AM 0
Share

Hi, i am very new to this. Can you explain a bit more in detail how to do it? Where to attach the scripts and so on. Thanks!

avatar image Jwizard93 · Jul 11, 2017 at 01:55 AM 0
Share

Either also destroy the key upon the collision with player, or use a bool so that it will only decrement num$$anonymous$$eys once. Like

     if(col.gameObject.tag == "Player")
     {
          Destroy(gameOJect);
          door.num$$anonymous$$eys--;
     }

OR

 private bool hasBeenPickedUp = false;
 
 function OnTriggerEnter(Collider col)
 {
     if(col.gameObject.tag == "Player" && !hasBeenPickedUp)
     {
         door.num$$anonymous$$eys--;
         hasBeenPickedUp = true;
     }
 }

This might be C# and Unityscript mixed together but you get it.

avatar image chainylol Jwizard93 · Jul 11, 2017 at 02:01 AM 0
Share

@jwizard93 i really appreciate your help, but no, i dont get it. Are the scripts written in c#? I get this error: error CS1525: Unexpected symbol `if'

avatar image chainylol Jwizard93 · Jul 11, 2017 at 02:03 AM 0
Share

I think i will need a full script to be able to understand this, i am still a beginner.

avatar image Jwizard93 chainylol · Jul 11, 2017 at 02:16 AM 0
Share

co$$anonymous$$g right up my friend.

Show more comments
avatar image
0

Answer by Jinkata · Jul 10, 2017 at 06:27 PM

You could use tags for your keys. Each key GameObject would be set with the 'key' tag and when you collide with the door simply use FindGameObjectsWithTag("key") and if the result length is > 0 you know there are still keys.

https://docs.unity3d.com/Manual/Tags.html https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html

You could also make a more complex system that strictly link the keys necessary to the door that needs them and the door would check itself to see if the keys are still there. That way you could have more than one door, each requiring a different set of keys.

You could put something like this on the door GameObject:

 OnColliderEnter(Collider other){
     if(other.col.gameObject.tag == "Player" && GameObject.FindObjectsWithTag("key").length == 0) {
         Destroy(this.gameObject);
     }
 }

And of course be sure the set the tag for the key GameObjects to the proper tag.

Comment
Add comment · Show 6 · 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 chainylol · Jul 10, 2017 at 06:36 PM 0
Share

Thank you for your answer. I am still very new to this, any chance you could show me how i can use the "FindGameObjectsWithTag("key")" in my code?

avatar image Jinkata · Jul 10, 2017 at 07:34 PM 0
Share

@chainylol I have update the answer for you. Does that clarify what I mean?

avatar image chainylol Jinkata · Jul 10, 2017 at 07:44 PM 0
Share

Once again, thanks. However, wouldn't that make it so i have to collect all the keys, and then collide with the door to destroy it? thats not what i want. I want the door to get destroyed once i pick up the final key, but maybe i didn't understand your code quite right.

Something like this maybe?

if (GameObject.FindObjectsWithTag("key").length == 0) { Destroy(this.gameObject); } }

avatar image chainylol Jinkata · Jul 10, 2017 at 08:10 PM 0
Share

And by the way, isnt it OnCollisionEnter? Not OnColliderEnter. heeelp :/

avatar image Jinkata · Jul 10, 2017 at 10:25 PM 0
Share

Yes, @chainylol you are correct. Did it work this way?

avatar image chainylol Jinkata · Jul 10, 2017 at 10:42 PM 0
Share

I did not get it to work unfortunately. How would you write the code if you were in this situation?

avatar image
0

Answer by chainylol · Jul 10, 2017 at 07:08 PM

@jinkata please elaborate on your first solution with FindGameObjectsWithTag, im still very new to this. Thanks!

Comment
Add comment · Show 9 · 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 Cornelis-de-Jager · Jul 10, 2017 at 10:46 PM 0
Share

You can also use a list system. See the code below:

$$anonymous$$eyCode

 // In the inspector drop and drag the linked door object here
 public Transform linkedDoor; 
 doorScript ds;
 
 void Start () {
       ds = linkedDoor.GetComponent<DoorScript> ();
       ds.Add$$anonymous$$ey (transform);
 }
 
 void OnCollisionEnter (Collision other) {
     // Remove the $$anonymous$$ey
     ds.Remove$$anonymous$$ey (transform);
 
     // Destroy this key
     Destroy (this,gameObject)
 }

DoorScript Code

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class DoorScript : $$anonymous$$onoBehaviour {
 
   List<Transforms>  keyList;
 
   void Start () {
      keyList = new $$anonymous$$eyList ();
   }
 
   public void Add$$anonymous$$ey (Transform key) {
       keyList.add (key);
   }

    public void Remove$$anonymous$$ey (Transform key) {
       keyList.Remove (key);
 
      if (keylist.Count == 0) {
          Destroy (this.gameObject);
       }
    }
 }

avatar image chainylol Cornelis-de-Jager · Jul 10, 2017 at 11:15 PM 0
Share

Hey man, thanks for taking your time to answer. The code looks great to me, but i get these two errrors when pasting in the $$anonymous$$eyCode script: https://gyazo.com/421750a0d00138b0daa47370d0268ede

DoorScript seems fine though.

avatar image chainylol chainylol · Jul 10, 2017 at 11:31 PM 0
Share

@Cornelis-de-Jager well actually it is just one error. accidentally made two scripts, sorry :D

Show more comments
avatar image
0

Answer by Cornelis-de-Jager · Jul 11, 2017 at 12:35 AM

Hey Mate. Here is the code as best I could Manage, fixed syntax errors. If you still have errors let me know.

Key Code

 using UnityEngine;
     using System;
     
     public class Key : MonoBehaviour {
     // In the inspector drop and drag the linked door object here
      public Transform linkedDoor; 
      doorScript ds;
      
     void Start () {
            ds = linkedDoor.GetComponent<DoorScript> ();
            ds.AddKey (transform);
      }
      
     void OnCollisionEnter (Collision other) {
        // Remove the Key
        ds.RemoveKey (transform);
      
     // Destroy this key
        Destroy (this.gameObject);
      }
     }



DoorScript Code

  using System;
  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class DoorScript : MonoBehaviour {
  
    List<Transforms>  keyList;
  
    void Start () {
       keyList = new KeyList ();
    }
  
    public void AddKey (Transform key) {
        keyList.add (key);
    }
 
     public void RemoveKey (Transform key) {
        keyList.Remove (key);
  
       if (keylist.Count == 0) {
           Destroy (this.gameObject);
        }
     }
  }




Comment
Add comment · Show 7 · 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 chainylol · Jul 11, 2017 at 12:47 AM 0
Share

Thanks for taking your time with this. I tried the code and i do get 1 error:

Assets/$$anonymous$$eyCode.cs(8,5): error CS0246: The type or namespace name `doorScript' could not be found. Are you missing an assembly reference?

Name of scripts: $$anonymous$$eyCode, DoorScript

avatar image Cornelis-de-Jager chainylol · Jul 11, 2017 at 12:50 AM 0
Share

In the $$anonymous$$eyCode script find:

public class $$anonymous$$ey : $$anonymous$$onoBehaviour

and replace it with:

public class $$anonymous$$eyCode : $$anonymous$$onoBehaviour

avatar image chainylol Cornelis-de-Jager · Jul 11, 2017 at 12:55 AM 0
Share

@Cornelis-de-Jager Did not solve it. Same error remains.

Does it work fine for you?

Show more comments

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

121 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

How can I target/attack all gameobjects in a certain area? 1 Answer

How to assign a color to a game object & destroy it when it collides with another game object of same Color 1 Answer

How to move a Game Object from a script not attached to it. 1 Answer

collisions in character not working! 2 Answers


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