Game Dev, Misc

Unity editor vs iOS

While testing the game on iOS I figured out that some behaviours really differs! I was investigating an issue lately that I could only replicate on mobile. And the outcome is quite interesting for me.

I have this piece of code on a “Gun.cs” script.

audioSource.PlayOneShot(fireClip, 1f);

Nothing fancy. This line will simply play a clip without looping and with a volume set to 100%.

The Gun script is used for all the guns of the game. And lately some new enemies I created were failing to fire on mobile but not in the editor. Actually they were shooting once and then stopping while in the editor they were shooting at will.

After debugging with Xcode it appears that I simply forgot to add the AudioSource component to their weapons. So the audioSource property was simply null. I’m even surprised this was working in the editor. Even though there is a big chance I didn’t check the warnings there, the behaviour was totally different. It’s really worth testing on mobile!

Still, there is a really neat trick to make sure you never forget required components with your scripts:

[RequireComponent(typeof(AudioSource))]
public class Gun : MonoBehaviour
{
}

This will add the AudioSource component to your GameObject for you.

I felt like writing something for Unity devs for once 🙂
Maybe once I released the game, I’ll do some tutorials!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s