|
Post by baroquedub on Jan 22, 2017 23:57:43 GMT
I'm calling the following in the death method of my Player's health script:
MasterAudio.FireCustomEvent("DoMusicFadeOut", this.gameObject.transform); 'DoMusicFadeOut' is defined in MasterAudio custom events (and successfully changed the music playlist) This worked for a little while but all of a sudden I'm starting to get errors finding the Transform:
MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it. The gameObject that the health script is attached to is still there in the scene, it isn't being destroyed. Can't think why I'm suddenly getting the error. I obviously have changed something without realising but can't work out what. Anyone got any ideas?
Also, would it be possible to fire a custom event without this transform? I realise it can be useful for some custom events types but why is it always required?
|
|
|
Post by baroquedub on Jan 23, 2017 0:03:28 GMT
.... in case it matters, I should mention that MasterAudio is started in another scene and set to 'Persist across scenes'
|
|
|
Post by DarkTonic Dev on Jan 23, 2017 5:48:36 GMT
Maybe you have a Custom Event that fires "OnDisable" or some other event that happens when the Scene ends, and the object is destroyed, yet instantly recreated in the new Scene so you think it's not destroyed? I don't think Unity is lying to you, it just appears that way.
Anyway I can't really troubleshoot this. I assume the object really is destroyed and you'll need to figure out why it doesn't look so.
Transform is required to get the position of the event origin, among other things. Not going to add another method that doesn't require the Transform, I want to keep it simple (one way to do it).
|
|
|
Post by baroquedub on Jan 23, 2017 12:06:21 GMT
Yeah, it's definitely something stupid in my code somewhere... thanks for the pointers
|
|
|
Post by DarkTonic Dev on Jan 23, 2017 17:45:58 GMT
I'm willing to bet it happens on Scene change or when something is destroyed. If that's an actual error in the Console, you can do "pause on error".
|
|
|
Post by baroquedub on Jan 25, 2017 0:12:20 GMT
Never spotted 'Pause on error' before - it's super useful! So here's the state of things when the error occurs. The scene doesn't change for a while. That's the whole point of my DoMusicFadeOut event - I want the playlist to change before the scene change, just as the player dies a Canvas Group is fade in and music should die out. What I don't get is that the console quite clearly shows that the error occurs on line 65 of the PlayerHealth.cs script. This line is the custom event being fired (if I comment out the line the error doesn't occur) You can see the script in the inspector, it's on the Head gameObject and is clearly not inactive. What gives, why is this.gameObject.transform nulll?! (ps. I totally appreciate you're not here to debug other peolpe's code I'm just struggling to get my head around why it's happening. Is there possibly a MasterAudio workaround I could use to change the playlist, without needing a reference to a transform?
|
|
|
Post by DarkTonic Dev on Jan 25, 2017 0:27:54 GMT
Try the following instead of this.gameObject.transform:
1) this.transform 2) this.GetComponent<Transform>()
Otherwise I'd say instead of firing a Custom Event, just do whatever the Custom Event does instead, since Unity is being weird. i.e.
PlaylistController.Instances[0].FadeToVolume(0f, 5f);
That doesn't require a Transform.
Also, just because the game object is still there doesn't mean it's not getting destroyed next frame. Pause on error stops it possibly before that happens... I think you should ask on the Unity forums why this is happening anyway. Really weird.
|
|
|
Post by baroquedub on Jan 25, 2017 0:39:18 GMT
You're a star, as always!
Bypassing the custom event with just:
PlaylistController.Instances[0].StartPlaylist("MenuMusic");
did the trick.
|
|
|
Post by DarkTonic Dev on Jan 25, 2017 1:01:16 GMT
Keep it simple! Always does the trick.
|
|