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 GregIven · Jun 28, 2016 at 05:54 AM · scripting problemgetcomponentif-statementsbug-perhaps

Exiting an if statement when calling a function from another script (Possible bug)

Hi, in my code I have an If statement that's conditional is based on the currentBaseState of my mecanim controller and currently the statement returns when it reaches a call to a separate script?

 if (currentBaseState.fullPathHash == hashes.attackState)
         {
             Target.GetComponent<EnemyScript>().GetHit(damage);
             impacted = true;
             runAttacked = false;
             Debug.Log("Boom");
         }

The Target is a transform for the enemy and it calls the enemy script to take damage. The impacted and runAttacked are just bools and the Debug.Log("Boom") is just to register the conditional in my console.

Is this a bug or am I not understanding how an if statement works with calling another function from a separate script?

Please help!

Comment
Add comment · Show 4
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 NoseKills · Jun 28, 2016 at 06:08 AM 0
Share

So the problem is the 'boom' doesn't log to console ?

It will only return if there's an exception like a null ref exception in that mehod calling line.

avatar image GregIven NoseKills · Jun 29, 2016 at 03:08 AM 0
Share

No, the bools after dont get set either... It works just fine if the GetComponent line is commented out...

This doesnt make sense

avatar image GregIven NoseKills · Jun 30, 2016 at 03:33 AM 0
Share

Sorry Nose$$anonymous$$ills, you were correct as well

avatar image _Yash_ · Jun 29, 2016 at 01:43 PM 0
Share

is it surrounded by try-catch ?

1 Reply

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

Answer by Bunny83 · Jun 29, 2016 at 02:39 PM

Well, if the booleans are set and the Debug.Log shows up when the line is commented out, but aren't executed when it's not that means that line throws an exception. However as Yash said in the comment above you wouldn't notice this exception if you catch it manually with try catch or if it's executed inside a coroutine. If you don't you should see the exception in the console.

All that information is missing in your question. So you should provide more context. Where is that snippet of code located.

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 GregIven · Jun 30, 2016 at 03:26 AM 0
Share

Ah wow, thanks Bunny83, so the problem was I had no attached the Enemyscript to GameObject that I was attacking. Once I did that it worked great.

Also, I made the mistake of collapsing the Errors in the console so I was only seeing the Logged 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

55 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

Related Questions

Unity GetComponent 1 Answer

How can I access another script in my prefab to set a variable after my network player setup runs? 1 Answer

Neater way to test an Enum in a different script? 1 Answer

How can I run an if check only after the first key press? 1 Answer

How to assign an image to an object at level 2 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