How do I activate game object B when clicking Game object A

Hi,
I have two game objects: Game object A and B. I want game object B to activate or enable when I click game object A with C Sharp script.

Sounds simple right? Well Im still stuck with this issue. I’ve tried the following:

    void start () {
                GameObject.Find ("SpriteController").active = false;

        }

    void Update () {

        if (Input.GetMouseButtonDown(0))

            GameObject.Find("SpriteController").active = true;
    }
}

“SpriteController” is object B and this script is on object A. Can anyone help?

you must define

void OnMouseDown() {

}

it could be that GameObject.Find doesnt find inactive objects, at least in the past that used to be the case it seems, based on a quick search. btw Start function has to start with a capital letter, and ive heard GameObject.Find is pretty slow, so it might not be the best way to do things like this while the application is running

i dont have unity on this computer so cant test but try this:

public GameObject b; //this will show up in the inspector of the GameObject that has this script, you need to drop your GameObject b on it there

void Start ()
{
        b.active = false;
}
void Update ()
{
        if (Input.GetMouseButtonDown(0))

            b.active = true;

}
1 Like

just read this: ‘‘I want game object B to activate or enable when I click game object A with C Sharp script’’

do you mean you have GameObject A in your game, for example a cube, and if mouse cursor is on it and you click, only then you want to activate GameObject B?

Yes thats exactly it.

Thanks guys, I’ll get to it tonight and try what you’ve advised.

Just reference the objects directly or find via tag and not name, GameObject.Find can only search for active objects.

Also it is terrible to use GameObject.find especially in your update.
So either use public GameObject fields to store a reference via the inspector click and drag, do find the objects in your start method and store them in a variable for later use.

1 Like

Got it. Yes the GamObject.find creates an error message that says there is no instance of the game object if the game object isn’t activated.

I suppose this would work if this script is on object B. I need it to be on object A and then activate object B on mouse down

in that case, cast a ray from camera to mouse position (there should be plenty of info on how to do it), and if it hits GameObject a and you click, set GameObject b active

1 Like

He’s would work if on A. If you put his script on object A and look in the inspector, there will be a slot to drag object B onto it. And the script will work on any object you drag into that reference

1 Like

Okay, well Ill try everything but this seems like the simplest solution.

using UnityEngine;
using System.Collections;

public class MouseClick : MonoBehaviour {

    GameObject myObject;

    void OnMouseDown() {
        myObject = GameObject.Find("SpriteController");
        myObject.renderer.enabled = !myObject.renderer.enabled;
    }
}

It works with

public GameObject b; //this will show up in the inspector of the GameObject that has this script, you need to drop your GameObject b on it there
void Start ()
{
        b.active = false;
}
void Update ()
{
        if (Input.GetMouseButtonDown(0))
            b.active = true;
}

:smile: Thanks a lot guys!