# Unity Unity3d glitch?

This topic is 2261 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So I've been writing a program in Unity3d in JavaScript, and for some reason, on one particular if statement, it executes some of the code, despite the fact that it should not execute that code. I have tried to look for every possible reason that could cause this, but I know for certain that nothing in the program is causing certain statements to change. Is it possible that Unity3d has some internal error? If so, can it be corrected? Thanks for any help!

##### Share on other sites
 var a = 0; if ( a = 1 ) alert ( "It's because it should be 'a == 1'"); 

##### Share on other sites
Post your code, but no I'm pretty sure Unity can handle if statements just fine.

##### Share on other sites
This is the code that has issues:

 if (responses[theBest]>winningResponse){ winningInputWeights1=inputWeightSet1[theBest].weights; winningResponse=responses[theBest]; } 

The program always executes the first statement in the if, but executes the second statement when its supposed to. Edited by kryotech

##### Share on other sites
Could you provide context for the code?

Also have you tried adding a print(winningResponse); before and after the if statement?

##### Share on other sites
The code above is used to update an array (the winning input weights) after the best set of weights for a neural net have been found. After the winning weights have been updated, 3 more random weight sets are created with the winning weights as a starting point. I have monitored how many times the if statement has been executed, and done a print(winningResponse) type thing in Unity3d to see what is changing and what is not. The winningResponse only changed when the if statement is called. The winningWeights, however, change regardless, which should not happen.

EDIT:
Could it be that the fact that I'm trying to set an entire array equal to another array is messing with the program? Edited by kryotech

##### Share on other sites
I doubt that the error is in the code piece above. Could you provide the code for the entire function? with types on all the variables please.

If you are using == between two arrays, it'll compare by reference. If you want to see if the elements in the arrays are equal you need to compare them one at a time.

##### Share on other sites

If you are using == between two arrays, it'll compare by reference. If you want to see if the elements in the arrays are equal you need to compare them one at a time.

On a similar note, if you are using = (assignment) you are assigning a reference, not a creating a copy.

So doing LeftArray = RightArray does not copy the contents of RightArray to LeftArray, it simply makes them both point at the same thing, so you will not be able to change their elements independently. Edited by Sandman

##### Share on other sites
Ok, so I think I found the problem, it was the array assignment that was off. Thanks for the help!

1. 1
Rutin
26
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 10
• 10
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
631751
• Total Posts
3002087
×