Integer arrays not comparing properly.
What I'm trying to do:
I'm trying to achieve a "Smart Block" feature on my game, since it has some kind of level editor in it.
But what is a "Smart Block"?
A Smart Block would be a Quad that knows if there are any other quads touching it and on what side they are. And, based on this, it determines where it is and what it is (a corner, a side, surrounded/fill) and then it changes the material and rotations based on those informations.
And... Smart Block is the name I gave to this kind of function.
The Issue
But, and there's alway a but on these posts, for some reason it is not working. It detects the positioning allright, but never changes the material. Could anyone help me?
Thanks in advance!
Answer by Igor_Vasiak · Sep 15, 2017 at 12:44 AM
Okay, nevermind. I've just found a solution:
The issue was at line 48 (when I compare the two arrays). I solved it by comparing manually if the values were equal. Why I can't compare arrays with == or .Equals() I'll never know. (Maybe I find out some day).
Why do you ask a question and close it 16 $$anonymous$$utes later and prevent any further answer from being posted? Especially since you seem to not understand the cause of the problem.
Well... I closed it because I did solve my problem. Am I wrong on doing this? And thanks for explaining me why it didn't worked.
Answer by Bunny83 · Sep 15, 2017 at 12:55 AM
This line doesn't look like it makes much sense:
if (blockType[j].hits == hitChecker)
hitChecker is an array. An array is a reference type. When you compare an array to another array with == you only compare their reference not the content of the array.
int[] arr1 = new int[]{1, 2, 3};
int[] arr2 = new int[]{1, 2, 3};
if (arr1 == arr2) // this can never be true as arr1 and arr2 hold two completely seperate array instances.
If you want to compare the content of the two arrays you have to use a loop and do an element by element comparison.
Answer by ransomink · Sep 15, 2017 at 01:06 AM
private Material thisMaterial;
this variable is not set or used within the script. not sure if that is intentional.
besides that, the code: GetComponent<Renderer>().material = blockType[j].material;
will set the script's gameobject to the material set inside of the BlockType class. Is there a material set inside of the BlockType object being accessed from the array? I'd do a null check before setting it.
you'd also have to compare the value inside of both arrays hits
and hitChecker
to each other and not the array themselves...
Thanks, I'll remove this$$anonymous$$aterial from my script. And, yes, I'm getting the material via the BlockType method.