Okay, so this is very annoying.
I am trying to create a Projector via script, but this doesn’t seem to work.
The Projector class is just a script interface, therefor
new Projector()
does nothing.
When instantiating like this
var prj = Instantiate(Resources.Load("pfb_SelectionProjector"));
The Projector gets created, but I can’t use it since it’s just an “Object” .
The Doc tells me to use GetComponent, but GetComponent from what? I haven’t added the prefab Projector to anything, so how should I get it?!.
Do I really have to create a GameObject in the Editor and work around that way?
Is there another possibility?
GameObject projObject = new GameObject();
Projector proj = projObject.AddComponent<Projector>();
Or if you are using a prefab
GameObject projObject = (GameObject)(Instantiate(.....));
Projected proj = projObject.GetComponent<Projector>();
Components always need to be attached to a GameObject.
Thanks so much for the fast reply!
Stuff like this is not in the docs (at least not directly) or did I miss something?
I searched google etc. like 10 mins but asking here really gets me to the point.
Thanks.
e:
nvm
You probably would have been ok with your previous Instantiate code. In the docs you’ll note that it says Instantiate returns an object - so you just have to cast it explicitly. Why there is no generic version of Instantiate like there is with GetComponent is beyond me…