- Home /
Question by
$$anonymous$$ · Nov 13, 2014 at 04:37 PM ·
gameobjecttypecomponentsconversioncasting
Conversion of GameObject with Component to Specific Type
I have the following:
MyObjectType myObject = null;
GameObject myObject2 = stuff.Find("Name of MyObject2").gameObject as MyObjectType;
//MyObject2 contains the component MyObjectType
I want to do this:
myObject = myObject2;
But I cannot convert GameObject to MyObjectType. Any ideas on how to get around this?
Comment
I'm guessing you want to get the component in the GameObject you've found (if it's a $$anonymous$$onoBehaviour), and not actually convert the GameObject itself to your type. If so, you also need to call GetComponent() on the GameObject.
Not sure what you are wanting to do, so taking a guess...
GameObject tempGo = GameObject.Find("NameOfGameObjectNotComponent");
if(tempGo!=null)
myObject = tempGo.GetComponent<$$anonymous$$yObjectType>()
Your answer
Follow this Question
Related Questions
Class hierarchy, gameObject, Raycast 1 Answer
How to add a Script to a GameObject during Runtime [2016] 3 Answers
Creating a Script at runtime, and adding it to an object... 1 Answer
Will Instantiating prefab with many components slow my game? 1 Answer
Forcing a type conversion from an abstract class C# 3 Answers