Storing a Game Object Address as a Variable

Hello people. I’ve been working on a c# drag and drop class for my game, and I need to store the object that I’m dragging as a variable. While that’s easily enough done, I need to store a reference to the game object that’s in the other script, not copy its value and store that in my drag and drop script. In other words, when I modify this variable (located in drag and drop), I want it to affect the original game object, the address of which is still located in the script that is being referenced by drag and drop. Does anyone know how to do this?

A simpler way of stating my question is this: how do I reference just the address of a game object in the memory, and not the game object itself?

I don’t know what language you are using but if you are using c# your code will be as follows

using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour {
    public GameObject player;
    void Example() {
        player = new GameObject("Player");
    }
}

When you create a reference to a Unity object, it is already essentially a pointer. It’ll never copy unless you specifically tell it to. I’ve never wanted an address of something in Unity, are you sure you need an address? What specifically are you trying to accomplish, and exactly what is it doing wrong?

GameObject thing;

thing = otherScript.target;

thing.transform.position += Vector3.up; // moves the otherscript's target object up one unit

Edit: Are you wanting pass-by-reference?

	public static void HierarchyPicker<T>( ref T cur, string label, Validator<T> validate ) where T : Component {
		cur = null;
	}

With that you can do:

public class MyThing : MonoBehaviour {
  Transform myTarget;
  void Edit() {
    HierarchyPicker(ref myTarget, ////);
  }
}

When you call edit, then HierarchyPicker will set the value of your MyThing’s myTarget to null because that is how the ref keyword works.