- Home /
Using static typing for a Component
I am trying to type out a static typing for my Component variable. But the compiler is having trouble.
private var tempComp : Component;
//
function Awake() {
infoComp = Camera.main.GetComponent("CentralSavingScript");
infoComp.PlayerInfo();
}
This is giving me an error saying:
BCE0019: 'PlayerInfo' is not a member of 'UnityEngine.Component'.
I don't know what type I have to cast tempComp as in order to remain as a static type. Thanks for any help.
Answer by joseph2011 · Jul 28, 2011 at 09:50 PM
Ok, so with some serious researching and trying to change the way I've been doing things for seven months now. I think I've figured out a solution. Here is the revised code without error.
private var tempComp : CentralSavingScript;
//
function Awake() {
infoComp = Camera.main.GetComponent(CentralSavingScript) as CentralSavingScript;
infoComp.PlayerInfo();
}
No exceptions are thrown up and it works. So I'm guessing this follows the static type casting in unity.
Your answer
Follow this Question
Related Questions
can I set a variable for a component to null 1 Answer
Change image alpha of a raw image component stored as a variable 1 Answer
2D Animation does not start 1 Answer
...FirstPersonController.m_WalkSpeed is inaccessible due to its protection level 0 Answers
Transform handle for prefab and getting to a prefab script variable? 1 Answer