Length Array error

For some reason my code is the exact same as ricks but an error shows up that says ‘MusicPlayer’ does not contain a definition for ‘Length’ and no accessible extension method ‘Length’ accepting a first argument of type ‘MusicPlayer’ could be found (are you missing a using directive or an assembly reference?) so i tried defining the Length as an int but now my game keeps playing the audio everytime you crash so that it layers. someone help

heres my code:

using UnityEngine;

public class MusicPlayer : MonoBehaviour
{
int Length;
void Awake()
{
int numMusicPlayers = FindObjectOfType().Length;
if(numMusicPlayers > 1)
{
Destroy(gameObject);
}
else
{
DontDestroyOnLoad(gameObject);
}
}
}

You’ve got a couple of issues in here, the main one being that you’re not actually specifying the type that you are looking for in your FindObjectOfType. You need to have FindObjectOfType().

Here is the git commit for that class:

1 Like

Thank you Rick that helped!

Privacy & Terms