- Home /
I cant get script of a GameObject(Camera)
Hi, guys
Im trying to get a script of a GameObject(Camera) like this:
private RebeccaCam rebeccaCam;
...
...
...
void Start(){
this.rebeccaCam = GameObject.Find("camera").GetComponent("RebeccaCam") as RebeccaCam;
}
...but always return null...why?
The hierarchy:
player (prefab) -> camera (Camera)
Thx a lot...Im noob yet Thiago
Answer by AlucardJay · Feb 26, 2013 at 03:53 AM
I think your keyword this may be the problem, try removing that first.
Also you can GetComponent by type not string : GetComponent (); // already typecast to specific component RebeccaAim
As another suggestion, break down your Find and GetComponent to see where it is failing :
.
void Start()
{
GameObject rebeccaAimObject = GameObject.Find("REBECA_CHAR");
if ( rebeccaAimObject )
{
rebeccaAim = rebeccaAimObject.GetComponent < RebeccaAim > ();
if ( !rebeccaAim )
{
Debug.Log( "REBECA_CHAR was found but the RebeccaAim Component WAS NOT ...." );
}
}
else
{
Debug.Log( "REBECA_CHAR NOT FOUND ...." );
}
GameObject rebeccaCamObject = GameObject.Find("camera");
if ( rebeccaCamObject )
{
rebeccaCam = rebeccaCamObject.GetComponent < RebeccaCam > ();
if ( !rebeccaCam )
{
Debug.Log( "camera was found but the RebeccaCam Component WAS NOT ...." );
}
}
else
{
Debug.Log( "camera NOT FOUND ...." );
}
}
This is a very long-winded Debug, but should show where the Find and GetComponent is failing.
Hi, guys thx for your help...Look I code this:
GameObject mainCamera = GameObject.FindGameObjectWithTag("$$anonymous$$ainCamera");
if(mainCamera){
Debug.LogWarning("$$anonymous$$ain Camera is not null");
rebeccaCam = mainCamera.GetComponent("RebeccaCam") as RebeccaCam;
if(rebeccaCam){
Debug.LogWarning("RebeccaCam is not null");
}else {
Debug.LogWarning("RebeccaCam is null");
}
} else {
Debug.LogWarning("$$anonymous$$ain Camera is null");
}
...and the Log:
...and still doesent work...I´ll kill myself !!! :p
C# is not my native language, fixed the answer for C# version of typecast GetComponent. This problem is a real headscratcher. Crazy suggestion, try changing Start to Awake for a quick test (in case the camera is not yet initialized).
...does not work. I will write the code in javascript...but make no sense to me do this.
Thx guys a lot !!! And sorry about that
Ok, guys...i wrote the code in javascript language and solve the problem.
function Start () {
var cam : GameObject = GameObject.Find("camera");
if(cam){
Debug.LogWarning("$$anonymous$$ain Camera is not null");
rebeccaCam = cam.GetComponent(RebeccaCam);
if(rebeccaCam){
Debug.LogWarning("RebeccaCam is not null");
}else {
Debug.LogWarning("RebeccaCam is null");
}
} else {
Debug.LogWarning("$$anonymous$$ain Camera is null");
}
}
Thank you, guys for your time and patience!!
Really you shouldn't have to change languages just to use GetComponent. I have done some searching today and there may be another way :
http://answers.unity3d.com/questions/31958/i-cant-understand-the-syntax-of-getcomponent-in-c.html
http://forum.unity3d.com/threads/9343-How-to-use-GetComponents%28%29-in-C
the last post on the second link shows :
// uJS
var nextGO : GameObject = GetComponent(someScript).nextGO;
// C#
GameObject nextGO = ((someScript)GetComponent(typeof(someScript))).nextGO;
again, C# is not my native so unable to provide info on using typeof(foobar)
note: this question is not solved (I have removed this), future readers wanting to know how to use GetComponent in C# will search and expect an answer if this is found. Please don't edit the title as Solved anyway, accept correct answers by clicking on the grey tick to the left of an answer. http://video.unity3d.com/video/7720450/tutorials-using-unity-answers
Answer by $$anonymous$$ · Feb 25, 2013 at 08:57 PM
A much better way to get a script would be to create a variable of the script and assign it in the inspector
public RebeccaCam scriptRebeccaCam;
you can acces it by just doing:
someVariable = scriptRebeccaCam.somePublicVariable;
look here for more on accessing other game objects
Answer by numberkruncher · Feb 26, 2013 at 12:45 AM
Does the following work for you?
this.rebeccaCam = Camera.main.GetComponent<RebeccaCam>();
No, doesnt work !
I create javascript, c# class...but still does not working. I want get the script to change camera´s position to right to the left side of my character.
Sorry, Im noob...but its look so easy !!! ..its killing me ! :p
Is RebeccaCam
definitely added to your camera object?
Does your camera have the $$anonymous$$ainCamera tag? That would be needed for my above suggestion to work.
I tagged to $$anonymous$$ainCamera...and still doesnt work...sorry guys, I dont know what im doing wrong...and thank you, numberkruncher !!!
Answer by fafase · Feb 27, 2013 at 07:21 AM
In C#:
RebeccaCam script;
void Start(){
Camera cam = (Camera)FindObjectOfType(typeof(Camera));
script = cam.GetComponent<RebeccaCam>();
}
That will work if you only have one camera if you have many you need to find the exact one with:
RebeccaCam script;
void Start(){
GameObject cam = GameObject.Find("CamName");
script = cam.GetComponent<RebeccaCam>();
}
Or if you are looking for the Main Camera which is named as such by default:
RebeccaCam script;
void Start(){
script = Camera.main.GetComponent<RebeccaCam>();
}
EDIT: I just saw you want to move your camera,fetch the Transform instead
Transform camTr;
void Start(){
camTr = Camera.main.GetComponent<Transform>();
}
void LateUpdate(){
camTr.position = transform.position;
}
Answer by mewhy · Mar 04, 2013 at 08:27 PM
i had a similar problem and you dont have to change the code just look at this documentation. http://docs.unity3d.com/Documentation/ScriptReference/index.Script_compilation_28Advanced29.html
Your answer
Follow this Question
Related Questions
How to get a variable value from another script(C#)? 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Distribute terrain in zones 3 Answers
Using GetComponent in multiple scripts for same component? C# 2 Answers
Object Reference not Set to an Instance of an Object in C# (Closed) 1 Answer