- Home /
Converting object into script
Hey guys, I'm having an issue with this line of code, it's on the unity forums and it works fine in the C# examples but I get the error saying it can't implicitly convert a Unity.Object into a LevelStatus, which is the script, but the unity tutorials don't talk about this at all.
LevelStatus level = FindObjectOfType(typeof(LevelStatus));
Any ideas?
Answer by djmorrsee · Aug 15, 2011 at 02:47 PM
try this
LevelStatus level = (LevelStatus)FindObjectOfType(typeof(LevelStatus));
That "converting" is called casting. It actually doesn't convert anything in that case. The real object is of type LevelStatus but the returned value is the more general type "Object" so it has to be casted into the real type.
As far as i can remember the newer versions of Unity also have a generic version of FindObjectOfType.
LevelStatus level = FindObjectOfType<LevelStatus>();
I'm still using an older version of Unity so i didn't used it yet but i'm pretty sure i've seen it.
yeah I initially tried LevelStatus level = FindObjectOfType(); but it didn't work, says it needs 0 arguements, either way, casting it worked fine, danke.
Your answer
![](https://koobas.hobune.stream/wayback/20220612181156im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can I Access Objects or Scripts in an Additive Level? 2 Answers
Accessing other objects 2 Answers
Help in solving mistake in my code 1 Answer