Passing references between game objects

Hi guys,

I’ve had a look but haven’t been able to find anything quite regarding this topic. Most of what I’ve found has involved communication between components on a single game object whereas I want information between different game objects. I think explaining what I want to do and how I’m going about it would probably result in a better answer from you guys in case my train of thought it incorrect so here it goes.

Basically I’m trying to make a tactics game where players will click on their units and then select an area to move to. I’ve been investigating ways to detect hits based on mouse clicks and saw you could do it with raycasts from a camera. Some sample code made this seem easier than detecting a click position every frame and then checking that position. Here is what I’ve got for my main camera so far:

using UnityEngine;
using System.Collections;

public class ClickDetecter : MonoBehaviour {
    Camera camera;
    public GameManager gameManager; 
    // Use this for initialization
    void Start () {

        camera = GetComponent<Camera>();
        gameManager = GetComponent<GameManager>();
	// Update is called once per frame
	void Update () {
        if (Input.GetMouseButtonDown(0))
        { // if left button pressed...
            Ray ray = camera.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
                gameManager.HandleHit(ray, hit);


Now the problem is that I’ve got my gameManager object in another component with a few other scripts to help make my game work. This is why GetComponent does not seem to be grabbing anything. Now I guess I could copy the script into the Camera component but then I’m not sure how it would react, would it copy the instance or create a new one for example?

I guess from a coding perspective what I want to do it just pass a reference of the currently initialised game manager I have into the camera and then just use that, is this possible or will they all have to be on the same component?

One idea I’ve had is to just copy the rest of the scripts into the camera since the other game object <'m using for my scripts isn’t too important by itself but I’m just curious if there are any other better ways or if my train of thought completely is wrong.

The reasoning behind wanting my game manager to handle the hit is just because a lot of information is stored in there so I thought it would be easier to just pass in the hit information and deal with it through that avenue.

Thanks in advance


Please check trough your spelling when asking questions. You state

Now the problem is that I’ve got my gameManager object in another component with a few other scripts to help make my game work.

I guess you meant:

Now the problem is that I’ve got my gameManager component in another gameobject with a few other scripts to help make my game work.

If that’s the case, you can use

GameObject go = GameObject.Find("NameOfTheGameObjectThatHasTheGameManager");
GameManager gameManager = go.GetComponent<GameManager>();
gameManager.HandleHit(ray, hit);


GameManager gameManager = FindObjectOfType<GameManager>();
gameManager.HandleHit(ray, hit);

However GameObject.Find() and FindObjectOfType<Type>() is not so great in terms of performance. I recommend at some point you look into the singleton pattern which is great for managers.