I went back to my old project which was dated back Unity 4.6. The game has been already tested and was ready for release.
I upgraded the project to 5.3 and 5.4b. Almost all of my code are still working except for the collision of projectiles (arrows). Since this was working before, I’m pretty sure I’ve setup the colliders and rigid bodies correctly.
Anyway, here’s what’s happening. I noticed that objects with collider (BoxCollider2D) instantiated from prefab do not have a visible collider (via gizmo). This is weird since I intentionally made the collider bigger than the sprite since the sprite was so small and might miss.
I observed that the collider wasn’t being properly initialized. Here’s how I found out:
Instantiate the prefab via code.
Pause the editor before the supposed collision of the projectile.
Change the size parameter of the box collider. After this, the collider suddenly becomes visible with the correct size. Note that upon instantiation, it already has a value taken from the prefab.
The collider works if I directly drag the prefab to the scene.
After that realization, I was able to fix it by hacking the code. Right after the instantiation of the prefab, I started a coroutine which yields until the end of frame. This coroutine will simply change the size value of the box collider. It won’t work if I don’t wait for the end of frame.
My guess is it’s missing the initialization process for colliders. I don’t want to apply this hacky fix that I just did. I want to know where I’ve gone wrong. I want to align my fix on how Unity expects us to do it.
This is out of my knowledge base - but does this have anything to do with the change to Unity 5 where the code has to AddComponent<BoxCollider2D instead of just referencing Boxcollider2D?
From Unity 5 release notes - http://unity3d.com/unity/whats-new/unity-5.0
changes -
Removed quick property accessors, like .rigidBody, .rigidbody2D, .camera, .light, .animation, .constantForce, .renderer, .audio, .networkView, .guiTexture, .collider, .collider2D, .particleSystem, .particleEmitter, .guiText, .hingeJoint for modularization. Instead, use GetComponent to get references.
Thanks for the insight but I’ve been modifying the collider through the inspector. It’s already been my practice not to use quick property accessors. Even if I did, I’m manually caching the components on Start or Awake. I also rarely call AddComponent. My practice mainly revolves on taking advantage of prefabs (less code and easier to manage =p)
EDIT:
Also, I’ve been using Unity 5.0 since its release. It’s just that this was a project from year ago I had to attend to. I might have missed something in Unity 5 but I’m pretty sure I’ve covered most of the new stuff.
I found the source of the bug. I noticed that whenever I change the projectile’s rotation (using LookAt) after instantiation, the collider gets bugged. Here’s what I did:
GameObject go = GameObject.Instantiate(Projectile.gameObject) as GameObject;
go.transform.position = Actor.transform.position + new Vector3(spawnOffset.x, spawnOffset.y, 0.0f);
go.transform.LookAt(Vector3.up);
// go.transform.rotation = Quaternion.LookRotation(Vector3.up); // This also bugs out
I moved the rotation code somewhere else and it worked. This was working before Unity 5.x so I’m curious about the exact cause of the problem.