Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Bug Unity Error CS0026

Discussion in 'Scripting' started by TrexMatrix, Apr 7, 2024.

  1. TrexMatrix

    TrexMatrix

    Joined:
    Aug 8, 2023
    Posts:
    41
    Got this error, don't know how to fix it.
    Assets\Scripts\DoNotDestroy.cs(9,33): error CS0029: Cannot implicitly convert type 'UnityEngine.GameObject' to 'UnityEngine.GameObject[]'

    This the script:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class DoNotDestroy : MonoBehaviour
    {
    private void Awake()
    {
    GameObject[] musicObj = GameObject.FindGameObjectWithTag("GameMusic");
    if(musicObj.Length > 1)
    {
    Destroy(this.gameObject);
    }
    DontDestroyOnLoad(this.gameObject);
    }
    }

    any ideas on how to fix it?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,211
    I would pay attention to the difference between these two methods:
    upload_2024-4-7_11-26-40.png
     
  3. TrexMatrix

    TrexMatrix

    Joined:
    Aug 8, 2023
    Posts:
    41
    thx i just added an "s" and it fixed the error
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,517
    C# has the very helpful var keyword. You go from this:
    GameObject[] musicObj = GameObject.FindGameObjectWithTag("GameMusic");

    To this more concise version:
    var musicObj = GameObject.FindGameObjectWithTag("GameMusic");