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?


2 Answers 2


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!

  • 2
    \$\begingroup\$ 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. \$\endgroup\$ Jul 23, 2013 at 5:09
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – bummzack
    Jul 23, 2013 at 6:49
  • \$\begingroup\$ @bummzack feel free to take it \$\endgroup\$ Jul 23, 2013 at 11:28
  • \$\begingroup\$ 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. \$\endgroup\$
    – jhocking
    Jul 18, 2014 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).


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .