I have a very strange “bug” in my game. I have a gameobject which is moved by code. When playing in Unity editor the object is perfect on the ground, but when I build my game it suddenly floats in the air. See images below:
I’m gonna guess you need to look wherever pos1 and pos2 is being set.
Also, what if the track is lower, not that the powerup is higher?
You must find a way to get the information you need in order to reason about what the problem is.
What is often happening in these cases is one of the following:
the code you think is executing is not actually executing at all
the code is executing far EARLIER or LATER than you think
the code is executing far LESS OFTEN than you think
the code is executing far MORE OFTEN than you think
the code is executing on another GameObject than you think it is
To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
is this code even running? which parts are running? how often does it run? what order does it run in?
what are the values of the variables involved? Are they initialized? Are the values reasonable?
are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)
Knowing this information will help you reason about the behavior you are seeing.
You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene
You could also just display various important quantities in UI Text elements to watch them change as you play the game.
If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.
Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.
Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:
Thanks. I am not new to Unity and this problem has never occured before, this is super strange. To give some more information, the script is set on the object in the scene with the following parameters:
Somehow when I print pos1 and pos2 it seems like it jumps very fast between 10 and 11,5. 11,5 is nowhere in the script nor the inspector and I don’t use any other script on this object to move it around. I am completely baffeled…
Maybe something else moving it? Another MovePowerup instance? An animation? A Rigidbody? Someone else just moving it?? What happens if you disable MovePowerup? What if you copy MovePowerup onto a cube right next to it, 2 units away? Same behaviour? etc etc etc. just rip tear shred, you’ll find it.
You really should just bite the bullet and make yourself an animation for this thing to bob up and down… it gives you so much more artistic control over the curve motion and also gets you out of writing code.
I am in this same exact boat! I force myself to either use animations (or something like LeanTween) when I don’t truly need code control… and the more you do it, it really does get easier. The worst part is how animations barf files all over your project, so you have to kind of collect them very diligently, like make a Powerup folder and move all the files in there: animations, controller, etc. Otherwise you end up with a catastrophe of stuff and you can’t find what you need except by diligently following each reference out of the object to the animator to the states to the proper animations.
But really, it really is better than writing and debugging code for mindless movement!
I have a similar problem: I have a Particle System attached to a GameObject. Neither is supposed to move. On the push of a Button I Play the Particle System.
In the Editor the Particles appear from behind the GameObject, just as intended.
In the build the Particles appear at the top of the screen, almost out of view (always at the same position), while the GameObject stays in place. I have tried WebGL and Windows builds, they both had the problem.