x


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 ( http://answers.unity3d.com/search.html?redirect=search%2Fsearch&q=CS0176 ) 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

more ▼

asked Apr 04 '12 at 11:52 AM

Reverend Speed gravatar image

Reverend Speed
30 5 5 12

(comments are locked)
10|3000 characters needed characters left

2 answers: sort oldest

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

more ▼

answered Apr 05 '12 at 03:39 PM

declancostello gravatar image

declancostello
36

And that works. Perfectly. Thanks!

--Rev

Apr 06 '12 at 12:52 PM Reverend Speed
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Apr 04 '12 at 12:01 PM

Statement gravatar image

Statement ♦♦
20.1k 35 70 175

Went with

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

...as it seemed a little cleaner than asking the program to Find the camera.

However, this yielded three errors - the same errors I got when I tried

GameObject.Find("Main Camera").GetComponent(CameraControllerRevCsharp).cameraNodes.push(myNode);

... a little later.

The errors are as follows:

1) Assets/Scripts/NodeControllerRevCsharp.cs(26,42): error CS0119: Expression denotes a type', where avariable', value' ormethod group' was expected

2) Assets/Scripts/NodeControllerRevCsharp.cs(26,29): error CS1502: The best overloaded method match for `UnityEngine.Component.GetComponent(System.Type)' has some invalid arguments

3)

Assets/Scripts/NodeControllerRevCsharp.cs(26,29): error CS1503: Argument #1' cannot convertobject' expression to type `System.Type'

Line 26 refers to the Camera.main / GameObject.Find("MainCamera") line. If you can shine a light on this new and infuriating issue, I'd appreciate it.

Thank you for your note on the difference between gameObject and GameObject - very helpful and something which had caused confusion in the past.

Swiftly coming to the conclusion that converting JS scripts into C# is not the way to learn the language. =)

--Rev

Apr 04 '12 at 12:26 PM Reverend Speed

What kind of array are you using? If you are using Javascript Arrays, obviously they are only compatible with Javascript and cannot be declared within C#. You may be better off using an ArrayList, which is compatible with C# (for more information on types of arrays, look here).

Also, with ArrayLists, .Push is not a valid command, so you would need to use .Add.

I'm not so sure whether I'm correct here, because I've not used C# much, but I'll give it a go anyway. :)

Hope that helps, Klep ;)

Apr 04 '12 at 02:02 PM Kleptomaniac

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. =)

Apr 04 '12 at 07:11 PM Reverend Speed
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4155
x3001
x4
x2

asked: Apr 04 '12 at 11:52 AM

Seen: 1166 times

Last Updated: Apr 06 '12 at 12:52 PM