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

Check to see if GameObject exists

Discussion in 'Scripting' started by ATLAS-INTERACTIVE, Nov 8, 2015.

  1. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    I need to check to see if a referenced GameObject exists in the scene, but cannot do it by searching by name as it cannot have a unique name, so I am unsure of how to do this as all examples I have found are checking the object name which I cannot do.

    I am referencing a GameObject like so:
    Code (CSharp):
    1. public GameObject activeObject;
    and need a way to check if that SPECIFIC object, seeing as it was dragged in from the Heirarchy, is still in the scene or not.
     
  2. Matt-Roper

    Matt-Roper

    <Of The Graphics> Unity Technologies

    Joined:
    Oct 27, 2015
    Posts:
    106
    If it's referenced you can literally just do:

    Code (CSharp):
    1. if(activeObject) {
    2. DoStuff();
    3. }
    Hope this helps.
     
    allesman, FunniDino, dejce18 and 2 others like this.
  3. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    Doesn't this just check to see if there is an object in the reference slot?
    This is for an editor, and I need to tell the script that there is nothing there anymore if I use Destroy to delete the object.
     
  4. Matt-Roper

    Matt-Roper

    <Of The Graphics> Unity Technologies

    Joined:
    Oct 27, 2015
    Posts:
    106
    "referenced GameObject exists"

    IF it's referenced then the code I posted will return true... If there is no object referenced and you do "if(activeObject)" then it will return false.
     
  5. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    What I am looking for more is a way to check if the referenced object is in the scene or not.
     
  6. Lentaq

    Lentaq

    Joined:
    Apr 8, 2015
    Posts:
    57
    I don't know if this is exactly what you are looking for, but it is along the same lines:

    http://docs.unity3d.com/ScriptReference/GameObject-activeInHierarchy.html

    It should let you know if the object is active, but if you have things that are in the hierarchy and "turned off", then it probably wouldn't work for that.

    There is also ID # generated by Unity that may or may not help you:

    http://docs.unity3d.com/ScriptReference/Object.GetInstanceID.html

    The ID is unique to each created object, so that would help you be specific, as you said.
     
  7. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    Is it something like this you need?
    If not then I am sorry.
    Code (CSharp):
    1. public GameObject activeObject;
    2.  
    3. void Start()
    4. {
    5.     activeObject = GameObject.Find("ObjectYouAreCheckingFor");
    6. }
    7.  
    8. void Update()
    9. {
    10.     if(activeObject == null)
    11.     {
    12.         //Do something?
    13.     }
    14. }
     
    Bodau-King likes this.
  8. Matt_Gamer

    Matt_Gamer

    Joined:
    Jan 2, 2019
    Posts:
    2
    That would be "if (activeObject != to null)" right?
     
  9. Matt_Gamer

    Matt_Gamer

    Joined:
    Jan 2, 2019
    Posts:
    2
    It's not working any way. Not mine, not yours. It's weird.
     
  10. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    Hey,

    Wow this post is old. I did not expect that :)

    Yes, if you want to check if it is currently in the scene, it would be != as opposed to ==.

    Could you paste your code here? I assume it is the same as my previous post but just to make sure.
     
  11. samf1111

    samf1111

    Joined:
    Sep 18, 2019
    Posts:
    16
    what if it's not refrenced?
     
  12. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Can't do anything without a reference. Get one. Presumably your method of getting one will fail if it's not there.
     
  13. That1phillps

    That1phillps

    Joined:
    May 16, 2018
    Posts:
    10
    Thank you guys for this thread. I didn't declare a variable in mine. I just had it look for the object and used != null and it worked.

    if (GameObject.FindGameObjectWithTag("CupOne")==null && maxValue >= .7f && maxValue < 1.7f && yAtSet - yOfThrow > -2 && yAtSet - yOfThrow < 2)
    {

    BallSpeed = 2.84f;


    }
     
    ChunkySoup likes this.
  14. exsol

    exsol

    Joined:
    Apr 8, 2021
    Posts:
    1
    Had lot of troubles with this too, at first it worked only with tags, but then I figured out where was the problem

    // Checks if object Dog(Clone) exists on the scene. We can't use just "Dog", because when prefab creates a copy it's always named as PrefabName + (Clone).

    if (GameObject.Find("Dog(Clone)") == null)
     
    Roman_Keivan and gamedeveloper0 like this.
  15. MrGreenish

    MrGreenish

    Joined:
    Oct 20, 2019
    Posts:
    34
    This won't solve the problem. The variable activeObject could reference a gameobject that is not in the scene. So even if it wasn't null it could e.g. reference a prefab that doesn't exist in the scene.

    I haven't tested this but activeObject.activeInHierarchy should work, I think.
     
  16. aloften

    aloften

    Joined:
    Aug 25, 2017
    Posts:
    18
    This is probably bad to keep this thread alive, but I use a delayed function and it will fire off after my object is deleted sometimes. The way I check for deletion is this at the top of my function...
    Code (CSharp):
    1.  
    2.         if (this == null) {
    3.             return;
    4.  
    5.         }
    I do this so that my function will just die if the object is gone. If you are also doing a delayed function, I recommend this.
    Its simple, easy to read, and will solve the problem.
    This ONLY checks if the instance of that script is still existent.

    No extra reference needed if ran from the script on the gameobject that disappears. :) Hope it helps.