How to check/find if a Gameobject present under a Transform is hidden or active?

How to check/find if a Gameobject present under a Transform is hidden or active?

I am clicking a button accordingly models display in the scene.When I click the same button again the same model appears twice.How to stop this. i.e. If model/Gameobject already loaded then no need to load again.

I am clicking a button accordingly models display in the scene.When I click the same button again the same model appears twice.How to stop this. i.e. If model/Gameobject already loaded then no need to load again.

I am using Gameobject.find here.When I click one button models corresponding model comes and other model present in the scene disappears.At this time Gameobject.find will not work since other model is hidden.Any better solution.Please look at the code below too many if else. :)

private string _assetname;

public void LoadAsstBundles(int choice) { if (choice == 1) { _assetname = “Chair1”; } else if (choice == 2) { _assetname = “Chair2”; } else if (choice == 3) { _assetname = “Chair3”;

}


if (_assetsBundle == null)
{
    Debug.Log("Could Not load AssetBundles");
}
else
{


    if (GameObject.Find(_assetname + "(Clone)"))
    {
        Debug.Log("Already Loaded");
    }

    else
    {

        var asset = _assetsBundle.LoadAsset(_assetname);

        int childcounts = ParentTransform.childCount;

        Debug.Log("Asset name nw ==" + asset.name);


        Debug.Log("Asset Bundles Loaded");

        var go = (GameObject)Instantiate(asset, ParentTransform);

        int option = go.transform.GetSiblingIndex();


        int childcount = ParentTransform.childCount;

        for (int i = 0; i < childcount; i++)
        {

            if (option == i)
            {
                ParentTransform.GetChild(i).gameObject.SetActive(true);
                continue;
            }
            ParentTransform.GetChild(i).gameObject.SetActive(false);

        }

    }

}

}

c# unity3d augmented-reality arkit

shareimprove this question

edited 2 mins ago


asked 34 mins ago

zyonneo

4082720

add a comment

1 Answer

active

oldest

votes


0


I used the Parenttransform.find to get the child hidden objects.Bit lengthy.Any updated code are welcome.

public void LoadAsstBundles(int choice)
        {
            if(choice==1)
            {
                _assetname = "Chair1";
            }
            else if(choice == 2)
            {
                _assetname = "Chair2";
            }
            else if(choice == 3)
            {
                _assetname = "Chair3";

        }

if (_assetsBundle==null)
{
    Debug.Log("Could Not load AssetBundles");
}
else
{

   var asset= _assetsBundle.LoadAsset(_assetname);

    //if (GameObject.Find(_assetname + "(Clone)"))
    //{
    //    Debug.Log("Already Loaded");
    //}


    if(ParentTransform.Find(_assetname+ "(Clone)")== true)
    {
        Debug.Log("Already Loaded");

        int childcount = ParentTransform.childCount;
        for (int i = 0; i < childcount; i++)
        {

            if (choice == i)
            {
                ParentTransform.GetChild(i).gameObject.SetActive(true);
                continue;
            }
            ParentTransform.GetChild(i).gameObject.SetActive(false);

        }


    }

    else
    {


        Debug.Log("Asset Bundles Loaded");

        var go = (GameObject)Instantiate(asset, ParentTransform);

        int option = go.transform.GetSiblingIndex();
        _loadednames.Add(go.name);




        Debug.Log("Sibling index == " + go.transform.GetSiblingIndex());

        int childcount = ParentTransform.childCount;

        for (int i = 0; i < childcount; i++)
        {

            if (option == i)
            {
                ParentTransform.GetChild(i).gameObject.SetActive(true);
                continue;
            }
            ParentTransform.GetChild(i).gameObject.SetActive(false);

        }


    }

}

}


c-sharp unity

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

C. C++. C#. 차이점 알려드림. 5분 순삭. Explain C.C++.C#. Like I'm Five

C. C++. C#. 차이점 알려드림. 5분 순삭. Explain C.C++.C#. Like I'm Five. 아시죠? C는 C++.C# 줄인말이 아니라는거?

Dicey Issues in C/C++

C/C++ problems. If you are familiar with C/C++then you must have come across some unusual things and if you haven’t, then you are about to. The below codes are checked twice before adding, so feel free to share this article with your friends.

Learn how to perform CRUD operations using C Sharp

QuickStart: CRUD Operations in C Sharp. Learn how to perform CRUD operations using C Sharp for MongoDB databases.

Loops in C++ | For, While, and Do While Loops in C++

In this Video We are going to see how to use Loops in C++. We will see How to use For, While, and Do While Loops in C++.

Using isdigit() in C/C++

In this article, we'll take a look at using the isdigit() function in C/C++. This is a very simple way to check if any value is a digit or not. Let's look