- Home /
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.
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).
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
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.
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
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.
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!
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)
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.
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.
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
![](https://koobas.hobune.stream/wayback/20220612125244im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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