‘Branch’ cause gameobject cant be get correctly and solution

This is just a wrong I found by accidental,maybe it was caused by the wrong usage method,but I still try to find a solution and hope to help others with the same problems.

I’ll show you a simple graph structure below and demonstrate the problem

Obviously, in this udon graph, we can know that both T/F will print the name(abcd) of the gameobject which the current script is put. I set the input to true.
The console shows the results we want

Now,if I change true to false

No GameObject was obtained to Get name

Now let’s make some small changes on this basis

I delete the line of the true

it got the expected result again



When the result is false and T/F shares the same output, gameobject is always unavailable

Now I choose with random numbers, so that the probabilities of T and F are 1 / 2 each

It’s incredible that it outputs name for both T and f conditions.Although it got the correct result, it was obviously inconsistent with the previous argument.

After running many times, I finally saw what I wanted to see :joy:

When the first random number is 8, it is judged to be false and Get name is null,in fact, I knew the reason long before that, so I directly said the conclusion:After the occurrence of true,GameObject is successfully found and getname is obtained, and the subsequent false will directly use the GameObject obtained from the previous true

Let’s go back to the simple structure
True once followed by false once

When false debug uses getname alone, GameObject is not obtained


Therefore, in order to avoid errors, add a branch with the result of true before each branch, and then share the same obtained GameObject with this true. Is it feasible.
Of course not. This method is too cumbersome, and it is obviously useless when two branches want to obtain two different GameObjects.
So what should we do?

Do not use the same node after two branches


The answer is so boring and simple that it may makes u feel that the foreshadowing of the previous long discussion is meaningless.You may think it’s a waste of time to see here, because there will be no case where two branches use the same GameObject’s node.

However, this is not the case, and when it appears, you will spend a long time to find the root of the problem, and even spend a lot of energy in the wrong direction.

When I encounter the problem that I can’t get the GameObject correctly, I try various methods to debug, and even suspect that it is the problem of thread and running timing.When I finally found that the reason was that the solution was so simple, even though I had spent a lot of time before, and I still didn’t know why the if statement caused this problem.

I will reproduce my previous wrong template, and you will find that it is really difficult to detect the cause of the problem

This is a part of the Udon graph. My intention is to carry out four cycles. Each cycle starts by randomly selecting one of the five color tiles (this part is not in the figure), and after each selection, set its position to the position of the empty object I stored in the GameObject array.

After running, the result is like this

Here, I use random numbers to determine which interval it belongs to, so as to select the color. Black will be judged first. If it is false, I will continue to judge whether it is other colors

Here is the generation sequence of perfabs.The 1st is green, indicating that it is false in the first branch,so it didn’t get the first GameObject in the array,and generated in the center of the world.
The second is black, which is true in the first branch, so it successfully gets and sets its own position as the second GameObject in the array.
The third is green and the fourth is red. Neither of them can get true in the first judgment, so they can’t get the GameObject correctly. However, since black successfully obtained the second GameObject before, 3 and 4 used the position of the second GameObject,so the two of them overlap with black and are in position 2

It’s also easy to fix it by dividing the nodes into multiple separate nodes

Obviously, it’s not enough here, and a very interesting phenomenon has occurred. The first blue is not generated in the center of the world, but in the center of the circle (holding this udon graph), although I don’t know the reason. After that, the black is generated in the correct position, 3 and 4 overlap it.

Now we got the right result