0

I am having problems accessing some components from others - this is mainly due to a failure to understand the Unity architecture. I was assuming that every time you 'name' a component, in effect you create a new C# type in the same way that a class definition does - but maybe not ... .

Note I am trying to hook all this up programatically for various reasons - I do not want to drag and drop in the editor, two reasons: (1) in some cases in practice this can only be done at runtime, and (2) I need to understand the architecture anyway.

My main player has a script attached, and in that I want to access a sub-component of the main player, being a camera. Called - for example - 'MyMainCamera'.

In Start() in the main player script I am doing this:

GameObject    tmp = GameObject.Find("MyMainCamera");

MyMainCamera  camera = tmp.GetComponent<MyMainCamera>();

//  do something with camera ....

(There's error checking code left out here). The problem is the compiler throws an error "The type or namespace name 'MyMainCamera' could not be found (are you missing a using directive ...?)". That is just an example, in other cases I need to access components that are not sub-components of the main player.

When I do the same thing with a Unity defined type like Text, Dropdown etc - presumably defined in 'using UnityEngine;' it all works fine.

Not clear how to do this therefore for a type that I define.

1 Answer 1

2

Unfortunately, naming a gameobject in Unity doesn't create a new type.

So from my understanding you have a Camera component attached to a Player Gameobject that you are trying to get? In this case you should be trying to:

GameObject tmp = GameObject.Find("MyMainCamera");

Camera myMainCamera = tmp.GetComponent<Camera>();    

Where MyMainCamera should be the name of the GameObject containing the desired camera component.

Since the camera component class name is simply "Camera" and not MyMainCamera, what you can do is name your variable "MyMainCamera" if that helps, like I have in the code above.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, thanks, got it. Camera is the type. Since there is only one camera, managed to make it even simpler, being 'Camera myMainCamera = Camera,main;'.Really unclear about what one is really doing when creating scripts and components in Unity - the docs gloss over it - they don't mention code much if they can avoid it. When one creates a script, it has a class name - that is a type, but 'myPlayer' - what is that? Just a GameObject I guess. From which, if it has a script, one can get the script (class ScriptClass) with GameObject.Find("myPlayer").GetComponent<ScriptClass>()?
myPlayer would simply be the name of your GameObject (where the name is a just a string which is part of the GameObject class) - each GameObject can have its own name as to make it easy to differentiate between them. To answer your question - yes, if you have a GameObject named myPlayer the code you showed would successfully return the ScriptClass component from it.
Preferably though, I would search for certain GameObjects using tags or directly dragging them in the inspector, as searching using a string is quite unreliable due to the risk of typos/mismatched names, and making it impossible to rename your GameObject without changing your code.
Thanks, but doesn't the same argument apply to tags? They are just text strings too aren't they - OK you add them to a list, but still it is just a list of strings? Don't much like linking stuff up in the Editor: (a) never really sure what it has done and (b) there are plenty of things that can only be linked at runtime, so I feel I need to understand the underlying mechanisms.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.