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 /
  • Help Room /
avatar image
0
Question by ajewell503 · May 13, 2020 at 08:44 AM · collisionother script

Detect collision on an object and send the data to a game manager to display text and enable/disable objects on that level

Hello, In my game, I have my player kill all enemies present in a level and then have a passage open up and a text pop up. Within the passage there is a chest that is there that can be opened and presents text when close enough. When the chest is opened there is a key that falls to the floor and has another text appear. Finally when the key is picked up it shows up on the side of the players screen so he can walk up to the door and open it. This worked completely fine with a player that was used on the level. The script that is used has tons of public variables that I had dropped on the inspector to make it work. But when I ran my game from the start, I used DontDestroyOnLoad to bring over the same health and ammo count from the player from the last level. Causing the script that was attached to the player to no longer have all of the game objects attached to the script. So I tried moving the script over to an empty object called game manager. That didn't work though since there was no collision detection between my player and the blocks. So I wrote a code on my player that detects collision with the blocks and calls a function in the game manager script. Sadly nothing has been working and I am stuck.

Game Manager Script:

 public void TouchingChest(bool check)
     {
         Debug.Log(check);
         if (check == true)
         {
             chestImage.gameObject.SetActive(true);
             chestText.gameObject.SetActive(true);
             openChest = true;
         }
         else
         {
             chestImage.gameObject.SetActive(false);
             chestText.gameObject.SetActive(false);
             openChest = false;
         }
     }
     
     public void TouchingKey(bool check)
     {
         Debug.Log(check);
         if (check == true)
         {
             keyIamge.gameObject.SetActive(true);
             keyText.gameObject.SetActive(true);
             pickupKey = true;
         }
         else 
         {
             keyIamge.gameObject.SetActive(false);
             keyText.gameObject.SetActive(false);
             openChest = false;
         }
     }
     public void TouchingDoor(bool check)
     {
         Debug.Log(check);
         if (check == true)
         {
             openDoor.gameObject.SetActive(true);
             openDoors.gameObject.SetActive(true);
             useKey = true;
         }
         else
         {
             openDoor.gameObject.SetActive(false);
             openDoors.gameObject.SetActive(false);
             useKey = false;
         }


And here is the player script for the collision detection:

 public void OnTriggerEnter2D(Collider2D collider)
     {
         if (collider.gameObject.name == "NextSceneTrigger")
         {
             level.NextLevel();
         }
         if (collider.gameObject.name == "Chest")
         {
             Debug.Log("Chest");
             other.TouchingChest(true);
         }
         if (collider.gameObject.name == "Key")
         {
             Debug.Log("Key");
             other.TouchingKey(true);
         }
         if (collider.gameObject.name == "TheDoor" || collider.gameObject.name == "TheDoor2" || collider.gameObject.name == "TheDoor3")
         {
             Debug.Log("Door");
             other.TouchingDoor(true);
         }
     }
     private void OnTriggerExit2D(Collider2D collider)
     {
         if (collider.gameObject.name == "Chest")
         {
             Debug.Log("Chest");
             other.TouchingChest(false);
         }
         if (collider.gameObject.name == "Key")
         {
             Debug.Log("Key");
             other.TouchingKey(false);
         }
         if (collider.gameObject.name == "TheDoor" || collider.gameObject.name == "TheDoor2" || collider.gameObject.name == "TheDoor3")
         {
             Debug.Log("Door");
             other.TouchingDoor(false);
         }
     }

Ignore the NextSceneTrigger. If anyone can figure out whats wrong with this code or if there are any better alternatives that would be much appreciated. Thank you in advanced.

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

0 Replies

· Add your reply
  • Sort: 

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

267 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 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 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

How to snap a placeable object on wall & move on to other walls of a room? 0 Answers

How to detect collision of two moving characters? 1 Answer

Changing light color between players when they collide (BOLT) 0 Answers

Problem replaying exactly same movement 1 Answer

How do I get nav agents to not collide with each other? 0 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