Hello guys,
I am looking for some help related to this problem: give the following code:
var gg = GameObject.Find("i_SwapCameras");
print(gg ? "Found i_SwtichCameras" : "i_SwitchCma not found");
if(gg) {
var component = gg.GetComponent("i_SwapCameras");
print(component ? "Found i_SwapCameras" : "i_SwapCameras not found");
gg.GetComponent("i_SwapCameras").switchIso2Perspective();
}
return;
}
for whatever reason I get a NullReference Exception error at the GetCompoment(“i_SwapCameras”) which is a component of the object named “i_SwapCameras”. The script file is i_SwapCameras.js and it’s attached to the object but … for whatever reason the GetComponent returns an error :((
Any idea why?
Thanks.
GC./
Are you sure it’s referring to the component and not the function? What does your print return?
Edit: While I have seen script names being fetched in quotes before ,I personally always use:
var component : i_SwapCameras = gg.GetComponent(i_SwapCameras);
I’m not sure if it matters though.
-Patrick
What do your print statements return?
It seems your code will call the switchIsoToPerspective regardless of whether the first getComponent call succeeded or not…
I mean, there’s no return if component is null…
Btw, you don’t have to getComponent twice there… Since you’ve stored it in that component variable, you could just do
component.SwitchIsoToPerspective()
Hope ths helps
Cheers
The thing is that
the print returns that it can’t find the object i_SwapCameras, but it is in my hierarchy (see the attachd). I have to say that I named “i_SwapCameras” both to the GameObject and the attached script but I would be rather surprised if that is the problem.
Cheers,
GC.
sorry, I CAN find the object i_SwapCameras, but I CAN’T retrieve the attached component i_SwapCameras (which is a js).
Actually for whatever reason this codind solved the problem:
function onGUI_SetIsoViewCamera() {
var gg = GameObject.Find("i_SwapCameras");
print(gg ? "Found i_SwtichCameras" : "i_SwitchCma not found");
if(gg) {
var component = gg.GetComponent("i_SwapCamerasScript");
print(component ? "Found i_SwapCamerasScript" : "i_SwapCamerasScript not found");
component.switchIso2Perspective();
}
return;
}
Thanks everybody for the help!