- Home /
Problem with conversion type of GameObject (C#)
Hello all,
I'm currently working on a RTS game and I have a problem with the implementation of a Unit Manager that is supposed to (as the name suggests) manage all units that can be selected by a player.
Now the problem I have is the following: error CS0266: Cannot implicitly convert type UnityEngine.Object' to
UnitManager'. An explicit conversion exists (are you missing a cast?)
The code is as follows:
private static UnitManager instance;
public static UnitManager GetInstance(){
if(instance == null){
instance = GameObject.FindObjectOfType(typeof(UnitManager));
}
return instance;
}
The script is already attached to a GameObject inside the scene, I've already tried the solutions of previously answered threads on this error, I tried implementing it differently, read the manual and watched some tutorials online, but nothing seemed to work.
Any help would be greatly appreciated.
Thank you
Edit: I used the js script from the Generals thread as a reference.
Answer by whydoidoit · Jul 19, 2012 at 12:30 PM
You need to cast it:
instance = GameObject.FindObjectOfType(typeof(UnitManager)) as UnitManager;
Ah, the simplest of solutions always evade me. Thanks for the help
Your answer
![](https://koobas.hobune.stream/wayback/20220613074410im_/https://answers.unity.com/themes/thub/images/avi.jpg)