Need Music Manager script help in C#

Updated 4 days ago in [A] C# .Net
Share on Facebook0Tweet about this on Twitter0Share on Google+0Share on Reddit0
7 on April 16, 2017

Hi, I write this code in C# :

public Transform musicPrefab;

void Start() {

if(!GameObject.FindGameObjectsWithTag(“MusicManager”)){
Transform mManager = Instantiate (musicPrefab, transform.position, Quaternion.identity)as Transform;
mManager.name = musicPrefab.name;
DontDestroyOnLoad (mManager);
}
}

and i  am getting his Error:

Assets/Scripts/GameMaster.cs(17,32): error CS0023: The `!’ operator cannot be applied to operand of type `UnityEngine.GameObject[]’

Don’t understand what’s wrong going on here, does anyone know the solution pls HELP me..:(

  • Liked by
Reply
6 on April 16, 2017

Just like the error says, you can’t use the ! operator in this line: if(!GameObject.FindGameObjectsWithTag(“MusicManager”)) change it to: if(GameObject.FindGameObjectsWithTag(“MusicManager”) == null)

on April 16, 2017

Thank you:) for your help, now I am not getting the above error..but with that, i am getting a different type of problem, like my background music is not running whenever I am played the game.
I was flowing Brackeys “Make A Game” tutorial and am stuck in level 13:

https://www.youtube.com/watch?v=yicdpF5YnTo&list=PLPV2KyIb3jR4u6zeBY77WPj0KuFdmv84g&index=14

i am not able to find the solution, please help me.

Master
on April 16, 2017

Where are you starting to play the music?

on April 17, 2017

From the beginning of the game!
this is my background audio which I was attached to the empty Game Object called <_GM/GameMaster)>.
In that time it was working wall, but as moving on with the tutorial, I cut the Audio component from the< _GM> game object(according to the tutorial) and paste it in the new empty Game Object called <_MusicManager>with the TAG name “MusicManager”
then I create a prefab in the same and deleted <_MusicManager> from the Hierarchy.
Modified the <_GM> C# code and attached more lines of code there, which I was already shared.

<—-public Transform musicPrefab;—> //  in that script!
and with that, I also able to attached <_MusicManager> prefab under name of a ‘musicPrefab’ variable.
So, after doing all the steps according to the tutorial..in the end, my background music not running whenever the game is played.

//_GM/GameMaster Script:

using UnityEngine;
using System.Collections;
public class GameMaster : MonoBehaviour {
    //UI and Score display!!
    public static int currentScore= 0;
    public int visualScore = currentScore;
    public float offsetY = 40;
    public float sizeX = 80;
    public float sizeY = 25;

    public Transform musicPrefab;

    // Update is called once per frame
    void Start() {
        //visualScore = currentScore;
        if(GameObject.FindGameObjectsWithTag(“MusicManager”)==null){
            Transform mManager = Instantiate (musicPrefab, transform.position, Quaternion.identity)as Transform;
            mManager.name = musicPrefab.name;
            DontDestroyOnLoad (mManager);
        }
    }
void OnGUI(){
        GUI.Box (new Rect((Screen.width-sizeX)/2, offsetY,sizeX,sizeY), “Score: “+ currentScore);
    }
}

6 days ago

Well, I am sorry..I think I was bugging you! what I will do that I will fix the issue later, for now, I am moving to the next level ..Thanx! :)

Devoted
6 days ago

FindObjectsOfType never returns null, so the condition Job gave you will always return false…

It returns an array of all of the objects it found. If there are no objects, it will return an empty array, but it will never return null.

What you can do is check if the length of the array, but a better option would be to still use “!”, but instead of using FindGameObjectsWithTag, use FindGameObjectsWithTag. This will try to find the first object with the specified tag, and the condition will return true of the object wasn’t found.

4 days ago

Thanx for your reply..:) I got the solution!!

Show more replies
  • Liked by
Reply
Cancel