Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by GFFG, Jan 14, 2015.

  1. GFFG

    GFFG

    Joined:
    Nov 13, 2014
    Posts:
    224
    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:
    Code (CSharp):
    1.  
    2.     void start () {
    3.                 GameObject.Find ("SpriteController").active = false;
    4.  
    5.         }
    6.  
    7.     void Update () {
    8.  
    9.         if (Input.GetMouseButtonDown(0))
    10.  
    11.             GameObject.Find("SpriteController").active = true;
    12.     }
    13. }

    "SpriteController" is object B and this script is on object A. Can anyone help?
     
  2. jeun

    jeun

    Joined:
    Jan 14, 2015
    Posts:
    4
    you must define

    void OnMouseDown() {
    ...
    }
     
  3. jaasso

    jaasso

    Joined:
    Jun 19, 2013
    Posts:
    64
    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:

    Code (CSharp):
    1. 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
    2.  
    3. void Start ()
    4. {
    5.         b.active = false;
    6. }
    7. void Update ()
    8. {
    9.         if (Input.GetMouseButtonDown(0))
    10.  
    11.             b.active = true;
    12.  
    13. }
     
    GFFG likes this.
  4. jaasso

    jaasso

    Joined:
    Jun 19, 2013
    Posts:
    64
    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?
     
  5. GFFG

    GFFG

    Joined:
    Nov 13, 2014
    Posts:
    224
    Yes thats exactly it.
     
  6. GFFG

    GFFG

    Joined:
    Nov 13, 2014
    Posts:
    224
    Thanks guys, I'll get to it tonight and try what you've advised.
     
  7. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    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.
     
    GFFG likes this.
  8. GFFG

    GFFG

    Joined:
    Nov 13, 2014
    Posts:
    224
    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.
     
  9. GFFG

    GFFG

    Joined:
    Nov 13, 2014
    Posts:
    224
    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
     
  10. jaasso

    jaasso

    Joined:
    Jun 19, 2013
    Posts:
    64
    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
     
    GFFG likes this.
  11. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    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
     
    GFFG likes this.
  12. GFFG

    GFFG

    Joined:
    Nov 13, 2014
    Posts:
    224
    Okay, well Ill try everything but this seems like the simplest solution.
     
  13. jeun

    jeun

    Joined:
    Jan 14, 2015
    Posts:
    4
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MouseClick : MonoBehaviour {
    5.  
    6.     GameObject myObject;
    7.  
    8.     void OnMouseDown() {
    9.         myObject = GameObject.Find("SpriteController");
    10.         myObject.renderer.enabled = !myObject.renderer.enabled;
    11.     }
    12. }
     
    Last edited: Jan 14, 2015
  14. GFFG

    GFFG

    Joined:
    Nov 13, 2014
    Posts:
    224
    It works with
    Code (CSharp):
    1. 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
    2. void Start ()
    3. {
    4.         b.active = false;
    5. }
    6. void Update ()
    7. {
    8.         if (Input.GetMouseButtonDown(0))
    9.             b.active = true;
    10. }

    :D Thanks a lot guys!