Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to reference a game object that was created in the editor and added to the scene (not created dynamically). How can I reference this object in a script added to another gameobject?

For example I have a Mesh called QuantumCold_B (in the editor), and I try to get its position in a script to be added to First Person Player, but instead I get

Unknown identifier ('QuantumCold_B').

Here is my script:

function Update () {
   transform.RotateAround(QuantumCold_B.position, Vector2,20 * Time.deltaTime);

Also is position the right way to get the Vector position on QuantumCold_B?

share|improve this question
up vote 4 down vote accepted

Since QuantumCold_B is not defined as a variable, you won't be able to use it that way.

If "QuantumCold_B" is the name of the object in the editor, you can use the GameObject.Find function to get the game object that has the name you want.

So, you would use:

var myObject : GameObject;
// This will return the game object named Hand in the scene.
myObject = GameObject.Find("QuantumCold_B");

Then, to get the position you'd use:


I suggest you familiarize yourself with the scripting reference in general to find a lot of these types of answers on your own. Good luck!

share|improve this answer
You can also create a GameObject member variable and assign it via the editor. This works when assigning an inactive object, while GameObject.Find() doesn't. – Panda Pajama Jul 23 '13 at 5:09
@PandaPajama I think this warrants a separate answer. The member-variable and assigning via editor approach is much more flexible and easier to read. Somebody looking for an answer to this problem should really have this as an answer. – bummzack Jul 23 '13 at 6:49
@bummzack feel free to take it – Panda Pajama Jul 23 '13 at 11:28
I agree. While the approach mentioned in this answer works, it is fairly brittle (eg. you can't reuse the script with other objects) and so the other approach is preferred. – jhocking Jul 18 '14 at 11:47

You should add a game-object as a public member to your class.

Let's assume you write a behavior for your Camera and name it CameraController. Here's how this could look like:

using UnityEngine;
using System.Collections;

public class CameraController : MonoBehaviour {
    // this will turn up in the Inspector where you can assign
    // an object to CameraTarget and use it later in your code.
    public GameObject CameraTarget;

    void Update () {
        // update position while looking at CameraTarget
        Vector3 pos = transform.position;
        pos.z += Time.deltaTime;
        transform.position = pos;

Once you add this script to your camera (or any other game-object for that matter), the CameraTarget it will show up in the "Inspector" as soon as you select this object. Then you can drag'n'drop any object from your scene or even your assets panel (think prefabs) to the Camera Target of your script.

Here's a screenshot how this looks like (added a Cube GameObject as the camera target):

unity inspector

This feature is not limited to Game-Objects. You can use it for almost any type, eg. use an int for hitpoints, Vector3 for velocity, etc. It's a really powerful concept and you should use it (you can also modify these values on the fly while running your game in the editor to test/tweak your game).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.