How to use GetComponenet in C#?

gameMaster is a script of mine…

masterObject is the gameObject the gameMaster is applied to.

gameMaster master = masterObject.GetComponent(typeof(gameMaster));

returns

error CS0266: Cannot implicitly convert type UnityEngine.Component' to gameMaster’. An explicit conversion exists (are you missing a cast?)

halp?

The output for that GetComponent() call is generalized to “Component”.

Try this…

gameMaster master = masterObject.GetComponent<gameMaster>();

This uses the generic method of obtaining a component and it will automatically cast the output for you.

Further reading can be found here: Unity - Scripting API: Component.GetComponent

(Just change the Javascript display to C# via the drop down menu on the code areas)

Hey thanks man that works =D

Glad it worked :smile:

Cheers and good luck.