Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 TerrarianAce · Jul 23, 2020 at 12:38 PM · gameobjectsetactiveonmousedowndebug.loggetkeydown

Making A Debug.Log depending on what GameObject is active

Hi! Sorry, but I'm really new to coding and am having a problem with something that is probably really simple. I have several items in my scene that can be "added to my inventory" (in reality they're just being disabled because I haven't implemented an inventory yet). I want to be able to press a button and get a description of the last object I "picked up." I have set up some invisible game objects, and "picking up" certain objects will turn these objects on and off. Depending on which invisble object is active, the O key is supposed to do a different thing. My code looks like this:

 private void OnMouseDown()
 {
       if (gameObject.name == "SmallRock01")
       {
            gameObject.SetActive(false);
            RockDescription.SetActive(true);
            StatueDescription.SetActive(false);
            KeyDescription.SetActive(false);
       }

       if (RockDescription.activeSelf && Input.GetKeyDown(KeyCode.O))
       {
            Debug.Log("The description of the rock goes here")
       }

 }

I have the same code set up for a small statuette and a key (thats why there are also objects called StatueDescription and KeyDescription) but with the right ones being active or inactive. Visual Studio isn't giving me any errors, but when I press O after "collecting" one of the objects nothing happens. Once again, sorry if my code is messy, I'm a beginner, and thanks in advance for all of the answers!

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
Best Answer

Answer by unity_ek98vnTRplGj8Q · Jul 23, 2020 at 04:40 PM

Try moving this code

 if (RockDescription.activeSelf && Input.GetKeyDown(KeyCode.O))
        {
             Debug.Log("The description of the rock goes here")
        }

out of the OnMouseDown() function and over to the Update() function so that it looks like this

 private void OnMouseDown()
  {
        if (gameObject.name == "SmallRock01")
        {
             gameObject.SetActive(false);
             RockDescription.SetActive(true);
             StatueDescription.SetActive(false);
             KeyDescription.SetActive(false);
        }       
  }
 
 private void Update(){
     if (RockDescription.activeSelf && Input.GetKeyDown(KeyCode.O))
        {
             Debug.Log("The description of the rock goes here")
        }
 }

The OnMouseDown function will only be called once on the frame that the mouse is clicked, but you are pressing the O key at some later time, so you need to continuously check for the O key every frame

Comment
Add comment · Show 1 · 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 TerrarianAce · Jul 24, 2020 at 01:38 PM 0
Share

Thanks for this! Worked an absolute treat :)

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

207 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

Related Questions

Conflicting Box Colliders 0 Answers

How to disable an object by clicking outside anywhere else? 1 Answer

how do i use gameObject.setactive with a bool?(my codes not working!) 3 Answers

Ok so im trying to get a string's name from another script to enable (SetActive) a certain gameObject in the scene (the player); however I keep getting errors and i have no idea what to do?? any help??? 1 Answer

SetActive true not working on game object,GameObject not reappearing after SetActive(true) was called on it 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