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

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

  • I think your keyword this may be the problem, try removing that first.
  • Also you can GetComponent by type not string : GetComponent < RebeccaAim > (); // 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.

Does the following work for you?

this.rebeccaCam = Camera.main.GetComponent<RebeccaCam>();

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; 
}

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

I had a similar problem where I was trying to assign a cameraMove variable to my CameraMove script:

private CameraMove cameraMove;

with the following line cameraMove would be null:

cameraMove = GameObject.FindGameObjectWithTag("MainCamera").GetComponent&ltCameraMove&gt();

but the following line worked fine:

cameraMove = Camera.main.GetComponent&ltCameraMove&gt();

turned out that somewhere I had accidentally tagged another object with MainCamera also -
this can easily be found with the following code

GameObject objs = GameObject.FindGameObjectsWithTag(“MainCamera”);

foreach (GameObject obj in objs)

Debug.Log(obj.name);