Cannot Implicitly Convert Type Object to Gameobject

Using the Unity API (which I do wish was more helpful than it actually is), I brushed up on finding an object automatically using tags, unfortunately using their code, exactly how they did it, doesn’t work, as do so many of the things in the API, if this is an out-dating issue, PLEASE update the API.

My main aim is to locate the GameObject in which I store all the players resource information such as resources, gold, inventory …etc.

using UnityEngine;
using System.Collections;

[System.Serializable]
public class BuildingStages : System.Object
{
    public string StageName;
    public GameObject[] StageObjects;
    public int StageCost;
}

public class BuildingSystem : MonoBehaviour {

    private Object ResourceManager;

    [Header("Building System")]
    [Header("Settings")]

    public BuildingStages[] Stages;

    public void Start()
    {
        if (ResourceManager == null)
        ResourceManager = GameObject.FindGameObjectsWithTag("PlayerManagers");
    }
}
private Object ResourceManager;

should be

private GameObject[] ResourceManager;

FindGameObjectsWithTag returns an Array of GameObjects, as noted in the documentation ( Unity - Scripting API: GameObject.FindGameObjectsWithTag). If you’re looking for one specific object, then use FindWithTag (Unity - Scripting API: GameObject.FindWithTag) and change your type to:

private GameObject ResourceManager;

You’re right - that documentation example is terrible.

FindGameObjectsWithTag returns a GameObject[ ] so Object (or Object[ ] for that matter) won’t work.

Thanks but it turns out this won’t work after all, I am trying to not only get the object but a specific script on that game object, which is proving harder than I first anticipated.
Using .GetComponent doesn’t seem to work at all, and leads to more problems.

It’s either just me or I am really good at finding just the really bad API examples.

using UnityEngine;
using System.Collections;

[System.Serializable]
public class BuildingStages : System.Object
{
    public string StageName;
    public GameObject[] StageObjects;
    public int StageCost;
}

public class BuildingSystem : MonoBehaviour {

    //private GameObject ResourceManager;
    private ResourceManager ResourceScript;

    [Header("Building System")]
    [Header("Settings")]

    public BuildingStages[] Stages;

    public void Start()
    {
        //if (ResourceManager == null)
        //{
        //ResourceManager = GameObject.FindWithTag("PlayerManagers");
        ResourceScript = GameObject.GetComponent(ResourceManager) as ResourceManager;
        //}
    }
}

GetComponent isn’t a static function like FindObjectsByTag (note the static functions in the documentation: Unity - Scripting API: GameObject), so you need a specific instance.

However, want you want to accomplish can be done using FindObjectOfType.

ResourceScript = GameObject.FindObjectOfType(typeof(ResourceManager));

You can also use the FindObjectsOfType if you need an array instead of the first result. It depends on how many ResourceManager scripts you have in your scene.

I find the documentation useful for finding useful methods and how to use them. In regards to using them, I usually debug through the error codes that unity generates. It tells you the line in the code and the problem with it. Ie, you were using an object but then trying to assign a gameobject to it.

Your problem right now is you are trying to get a component from the gameobject that this script is attached too, which it isn’t, its attached to the resourcemanager gameobject. Get component is a public function, which works on the gameobject to which it is attached. The find functions, are static functions, for which there would be one for all gameobjects, so it can search through them all.

Assuming you have already assigned the resource manager gameobject
(if you make it public, you can drop the prefab easily onto whatever this object is attached to. Or you can use gameobject.find(“name”) within your script to assign the resourcemanager gameobject)
then ResourceScript = resourcemanager.getcomponent();
otherwise I think it would probably be something like gameobject.find(“name”).getcomponent();

Hope this helps!

That documentation is actually good. GameObject isn’t the same thing as gameObject :slight_smile: