cs0176 - Trying to locate the Main Camera in order to add a value to an attached array

Current code in NodeControllerRevCsharp.cs:


using UnityEngine;
using System.Collections;

public class CameraNode{
		
		public Vector3 position;
		public float range;
		
		public CameraNode(Vector3 position, float range){
			this.position = position;
			this.range = range;
		}
	}

public class NodeControllerRevCsharp : MonoBehaviour {
	
	public float range;
	
	void OnDrawGizmosSelected() {
		Gizmos.color = Color.red;
		Gizmos.DrawWireSphere (transform.position, range);
	}
	
	void Start () {
		CameraNode myNode = new CameraNode(transform.position, range);
		gameObject.Find("Main Camera").GetComponent(CameraControllerRevCsharp).cameraNodes.push(myNode);
		//GameObject.Find("Main Camera").GetComponent(CameraControllerRevCsharp).cameraNodes.push(myNode);
		//Camera.main.GetComponent(CameraControllerRevCsharp).cameraNodes.push(myNode);
		//gameObject.Find(Camera.main).GetComponent(CameraControllerRevCsharp).cameraNodes.push(myNode);
	}
}

Resultant error message:


Assets/Scripts/NodeControllerRevCsharp.cs(26,28):
error CS0176: Static member
`UnityEngine.GameObject.Find(string)’
cannot be accessed with an instance
reference, qualify it with a type name
instead


C# newbie having a little trouble accessing my Main Camera in code - I’ve got this working in JS, so I assume the solution to this is just extraordinary ignorance. I’ve looked up the error on UnityAnswers ( Search results for 'CS0176' - Unity Discussions ) and either the existing answers are unhelpful or I just don’t understand 'em.

Previous attempts to solve the issue can be found near the end of the code, commented out. All yielded a further rats nest of errors.

Would anybody care to hazard a solution? And perhaps a bug-finding methodology so I’m not constantly bothering the good people of Unity Answers? =)

Thanks for reading,

–Rev

2 Answers

2

I’ve had a look at Rev’s code and after some experimentation …

Camera.main.GetComponent(CameraControllerRevCsharp).cameraNodes.push(myNode);

You need to get component by the name of the class so it needs to be in quotes “className”
It’s also possible to call GetComponent by type

http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponent.html

I’m quite new to c# but I know it’s a stickler for type. So we ended up GetComponent-ing as className and also making sure that the variable was the same type.

CameraControllerRevCsharp Rev = Camera.main.GetComponent("CameraControllerRevCsharp") as CameraControllerRevCsharp;

Rev.cameraNodes.Add(myNode);

There was also a problem that

CameraNode was defined in 2 different places, once in NodeController as a public class
and once in CameraController as a sub-class ( member variable ?)

We eliminated the class in NodeController
and changed

CameraNode myNode = new CameraNode(transform.position, range);

to

CameraControllerRevCsharp.CameraNode myNode = new CameraControllerRevCsharp.CameraNode(transform.position, range);

so that we are using the definition of the class from CameraController

And that works. Perfectly. Thanks! --Rev

Either use Camera.main or GameObject.Find(“Main Camera”).

GameObject.Find will find an active game object with the specified name.


gameObject and GameObject are two different things. gameObject refers to the game object the script is attached to and GameObject refers to the class and class methods associated with GameObject. GameObject.Find is a class method, not a member method.

Hi Klep! I'm using a Generic List (based on some previous problem-solving advice I received on this website) - this is the latest in a long line of array types I've drafted in to solve my problematic scripts (drawn from this page - http://robotduck.wordpress.com/2009/11/04/88/). You're absolutely right to 'push' me towards 'add', as Generic Lists don't accept 'push' either. I've made the fix and while this hasn't rectified my immediate issue - I still have those three errors in my comment above - I'm sure it will save me some more angst in the near future. Thanks a lot, man. =)