- Home /
Fixed it by myself.
Need help with fixing a code
Hey guys! I have been working on an endless runner since yesterday, and it's almost done. I am trying to make a Shop right now, but i have made so many scripts i am starting to get a little bit mad everytime i make a mistake, and this is a big one.
I have placed two arrows on the screen. One being LEFT and another being RIGHT. If you click LEFT, the game is supposed to switch your cars. It is supposed to make your current car return (Go left out of the screen), and make the next character go (Go from the right to the middle of the screen). So to make this work, i basically made two ints, and a coroutine. Everytime the player clicks on one arrow, it will check which car is the current, and then write which is himself, and which is the next one. Then i call a coroutine to make sure the current returns and the next goes. The problem is, the first time it works, but everytime after it, the next car goes, but the current car just won't return. What i am missing?
Here are the parts of the script you need to know:
private int car;
private int myNumber;
private int hisNumber;
public void Change()
{
if (isRight == false)
{
if (cars[0].activeSelf == true)
{
myNumber = 0;
hisNumber = 1;
StartCoroutine(Left());
}
else
if (cars[1].activeSelf == true)
{
myNumber = 1;
hisNumber = 0;
StartCoroutine(Left());
}
}
}
IEnumerator Left()
{
cars[myNumber].GetComponent<Animator>().SetTrigger("Return");
cars[hisNumber].SetActive(true);
cars[hisNumber].GetComponent<Animator>().SetTrigger("Go");
yield return new WaitForSeconds(0.5F);
cars[myNumber].SetActive(false);
}
Thanks for reading, i know it's a big question. Any answer would be appreciated, this is the last thing i need to make and then the game will be ready to be released. I know it's probaly something basic i am missing here but i just can't notice what it is right now...
Answer by Cynikal · Jun 18, 2017 at 10:00 PM
Out of curiosity... Wouldn't "hisNumber" to 0 instead of 2?
Nah, i don't like to just throw everything i coded to everyone, so i sent two parts of it. Let me rewrite it. Ins$$anonymous$$d of two cars, there are actually 4 and there is also the part of the right arrow, but as i said i don't like to throw all of my code away. Just edited it.
Ugh, dumb me... I knew it! I forgot to make the car return from the Go animation, so when it moved to be the current car, it would never return, that's why it only worked on the first one. Anyway, you tried so... question solved.
Follow this Question
Related Questions
Help with animator controllers 1 Answer
StateMachineBehaviour Issue when swapping runtimeAnimatorController 0 Answers
why I have to anim.getComponent in update() function when I had done in Start () function 2 Answers
Have some minor problems. Cant tell if its a script issue or a animator issue. 1 Answer
How to trigger udpate in editor mode only when I modify component parameters 1 Answer