Does my script become stupid, or do I become stupid?
Hi everyone, I've got a bad problem with my script.
public void OnCollisionEnter(Collision other)
{
if(other.gameObject.name == "down")
{
result = result -= 1;
print(result);
StartCoroutine(waiterDown());
}
if (other.gameObject.name == "up")
{
result = result += 1;
print(result);
StartCoroutine(waiterUp());
}
}
Here's the part of the code that I don't understand. If the object touches "up", it adds to the integer value "result" +1, but if it touches "down", it removes -1 to the integer value "result". BUT...! In the console, I see instead of "5, 4, 5, 4, 3, 2, 3..." this "0, 1, 2, -1, -2, 3.."!! That has no sense! If someone can help me, it'll great! I'll test all your propositions. 2 - 1 doesn't make -3!
And I forgot to say that I'm not a native English speaker, that's why my English isn't very good... Thanks!
Answer by Major · Jun 11, 2017 at 10:29 PM
I'm not 100% sure how this script works with your scene, but there are some unusual syntax parts that may have something to do with it.
On lines 5 and 11 you have this:
result = result -= 1 or
result = result += 1
This doesn't make sense because you are using assignment operators inside of an assignment operator. Anything with an '=' sign in it, is an assignment operator. In essence, you are assigning a value to result twice in each respective instance, and without more knowledge on how this works I have to assume that this might have something to do with it. What you mean here is this:
result -= 1 or
result += 1
or
result = result - 1 or
result = result + 1
This is really the only way I can help because I do not fully understand how this script is being interacted with, and how the functions in this script are used.
Your answer
![](https://koobas.hobune.stream/wayback/20220612123936im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do you add values of int healthValue on collision with a gameObject Coin in a 3d world?? 1 Answer
String to int conversion not working in ControllerCollider method although I am sure it is an int... 1 Answer
How to remove parts from a string 1 Answer
Sync Var not syncing on client. 0 Answers
NullReferenceException: Object Reference not set to an instance of an object 1 Answer