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 Corcolj · Jun 30, 2017 at 10:10 PM · not workingmethodfailure

Why the method doesn't run?

I'm a beginner so I'm having a couple of problems. First, I got this:

 if (Input.GetKeyDown(KeyCode.UpArrow))
                 {
                     flyReady(SpriteController.VIC_READY);
                 }

And below I implement this:

 public void flyReady(int i)
     {
         move = 1;
         gameObject.GetComponent<SpriteController>().setPlayerSprite(gameObject, i);
         if (gameObject.name.Equals("Víctor"))
         {
             gameObject.GetComponent<BoxCollider2D>().size = new Vector2(40f, 8f);
             gameObject.GetComponent<BoxCollider2D>().offset = new Vector2(0f, 0f);
         }
         if (gameObject.name.Equals("David"))
         {
             gameObject.GetComponent<BoxCollider2D>().size = new Vector2(38f, 9f);
             gameObject.GetComponent<BoxCollider2D>().offset = new Vector2(0f, 1f);
         }
             flying++;
         check = true;
     }

Then when the first if's condicion is satisfied the methodflyReady doesn´t work/run.

I'm not sure if this is relevant but:

  • VIC_READY is a const

  • David & Víctor are the names of two gameObjects that exist the whole time

  • check is bool,

  • flying & move are int

  • setPlayerSprite() is a method implemented in another class its parameters are an int (usually I use a pre-defined const) and the gameObject wich is going to change its Sprite

Thank you so much for your help and excuse me I've my English is too bad.

Comment
Add comment · Show 2
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 andrew-lukasik · Jun 30, 2017 at 10:51 PM 0
Share

Few words of coding advice. Calling "gameObject.GetComponent()" every frame is bad practice. It's both CPU inefficient, unclear to read, exception-prone eventually. Your code should look little more like this:

 public class $$anonymous$$yClass : $$anonymous$$onoBehaviour
 {
     
     [SerializableField] SpriteController mySpriteController;
     [SerializableField] BoxCollider2D myCollider;
     [SerializableField] Guy myGuy = Guy.undefined;
 
     public void Example$$anonymous$$ethod ()
     {
         if( myGuy == Guy.$$anonymous$$)
         {
             myCollider.size = new Vector2( 40f , 8f );
             myCollider.offset = new Vector2( 0f , 0f );
         } else if( myGuy == Guy.$$anonymous$$ )
         {
             myCollider.size = new Vector2( 38f , 9f );
             myCollider.offset = new Vector2( 0f , 1f );
         } 
     }
 
     public enum Guy
     {
         undefined = 0,
         Victor,
         $$anonymous$$,
         SomeOtherGuy
     }
 
 }

Just remember to fill component fields by hand in inspector (seems like additional work, but trust me - it's the best solution).

avatar image Corcolj andrew-lukasik · Jun 30, 2017 at 11:25 PM 0
Share

O$$anonymous$$, this is a big advise,

I'll try it when I have a break because I need to change a lot the code

Thank you Andrew

2 Replies

· Add your reply
  • Sort: 
avatar image
-3

Answer by bgprocks · Jun 30, 2017 at 10:25 PM

When you working with game objects, dont assume you are going to get back a proper string, even if you should. try" if ( (string)gameObject.name == "Victor" ). Forcing the output to a string for sure.

Comment
Add comment · Show 5 · 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 Corcolj · Jun 30, 2017 at 10:56 PM 0
Share

Hi bgprocks,

thanks for the help, I will keep your advice in $$anonymous$$d, but the problem was still. I made it a non-paramether method (public void flyReady()) and now it works.

Do you have any idea of what 's happening?

Thanks again

avatar image Bunny83 · Jun 30, 2017 at 10:59 PM 1
Share

This is just wrong and is misleading. Casting to a string would only work if that thing is already a string. A cast never performs an actual conversion unless you defined a conversion operator. GameObject.name is a property of type string. So no matter what you do it will always be a string. There is no case whatsoever where a cast would be necessary here.

avatar image Corcolj Bunny83 · Jun 30, 2017 at 11:14 PM 0
Share

I see, Bunny83, thanks for the advice

avatar image bgprocks Bunny83 · Jul 01, 2017 at 11:13 AM 0
Share

Trying to help same as you...no need to bash other peoples answer. We don't have the whole code so trying to work through whats going on. And I have been coding longer than you and seen weirder stuff than that!

avatar image NoseKills bgprocks · Jul 01, 2017 at 05:49 PM 1
Share

Don't take it personally. It's just that answers that don't make sense to others and seem to spread misleading info or create a sense of mysticism around program$$anonymous$$g SHOULD get downvoted so newbie programmers don't get mislead.

And I have been coding longer than you and seen weirder stuff than that!

$$anonymous$$akes me interested in what that "weirder stuff" is. In my experience, the more I learn about program$$anonymous$$g (and Unity), the less "weird stuff" I see, because I know why it happens.

If there truly is a reproducible case of .name not returning a string but still something that can be casted to one, I hope there's an open Unity bug reported about it.

And if someone proves this is a good answer that sometimes fixes a broken string comparison, I'll gladly upvote (I did not downvote)

avatar image
0

Answer by Bunny83 · Jun 30, 2017 at 11:11 PM

There is some important information missing. Where is that if statement if (Input.GetKeyDown(KeyCode.UpArrow)) located?

It should be inside Update. So your code should look something like this:

 void Update()
 {
     if (Input.GetKeyDown(KeyCode.UpArrow))
     {
         flyReady(SpriteController.VIC_READY);
     }
 }
 
 // [ ... ]

Some things you should check:

  • First of all, make sure you don't have any errors in the console.

  • Make sure your script is actually attached to a gameobject in the scene and that the script is enabled and the gameobject is active.

  • Try adding a Debug.Log("flyReady entered"); right at the beginning of your "flyReady" method. Does the log appear in the console when you press the up arrow ? If it does your method is executed. If not make sure you have spellt "Update()" correctly. If you named your method "update()" it won't be executed.

  • If that all doesn't help, try adding a Debug.Log temporarily inside Update to see if Update runs or not.

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 Corcolj · Jun 30, 2017 at 11:20 PM 0
Share

It's located into a method that is alredy attached to Update().

I' ve checked all the points and there're no errors, the script is attached to a gameObject, it's inside Update() and I checked if the variable flying changed ins$$anonymous$$d of using Debug.Log, also the Update() it's running, but it doesn't work.

avatar image Bunny83 Corcolj · Jul 01, 2017 at 01:40 AM 0
Share

You increase your flying variable at the end of the method. If there's an exception thrown before that line the method would be ter$$anonymous$$ated before you reach that line. Also it's possible that you may reset that variable from somewhere else. So it's generally a bad indicator if you actually reach the line or not.

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

69 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

Related Questions

OnMouseDown working only sometimes 1 Answer

Unity 2017.2 not working 3 Answers

Why cant I build and run my android project anymore 0 Answers

Collision not working 0 Answers

Download "Failed- No File" for Standard Assets and Example 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