/
/
/ "BRO TIGER"2017-09-12 11:30:01
C++ / C#
/ "BRO TIGER", 2017-09-12 11:30:01

Problem with adding prefabs to hierarchy, what to do [C#]?

Good day! I ran into a strange problem that I still can't solve... The fact is that not so long ago I took a more or less simple script from Unity Online Tutorial... The tutorial video itself gave me an idea on how best to create a store and maybe inventory. .. I noticed an interesting problem - Prefabs (Or their appearance) did not appear if the parent of the hierarchy was not active at the start of the game, but if it was active. then everything was displayed normally...
Thanks in advance for any help or support!

Store building tutorial video (Link)

https://www.youtube.com/watch?v=TAJCr3_kfEc&list=P...
Hierarchy

ScrollView -> Viewpoint -> Content - вот родитель!
Script 1 (From Tutorial Video)
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;

[System.Serializable]
public class Item
{
    public string itemName;
    public Sprite icon;
    public float price = 1;
}

public class ShopScrollList : MonoBehaviour {

    public List<Item> itemList;
    public Transform contentPanel;
    public ShopScrollList otherShop;
    public Text myGoldDisplay;
    public SimpleObjectPool buttonObjectPool;
    
    public float gold = 20f;


    // Use this for initialization
    void Start () 
    {
        RefreshDisplay ();
    }

    void RefreshDisplay()
    {
        myGoldDisplay.text = "Gold: " + gold.ToString ();
        RemoveButtons ();
        AddButtons ();
    }

    private void RemoveButtons()
    {
        while (contentPanel.childCount > 0) 
        {
            GameObject toRemove = transform.GetChild(0).gameObject;
            buttonObjectPool.ReturnObject(toRemove);
        }
    }

    private void AddButtons()
    {
        for (int i = 0; i < itemList.Count; i++) 
        {
            Item item = itemList[i];
            GameObject newButton = buttonObjectPool.GetObject();
            newButton.transform.SetParent(contentPanel);

            SampleButton sampleButton = newButton.GetComponent<SampleButton>();
            sampleButton.Setup(item, this);
        }
    }

    public void TryTransferItemToOtherShop(Item item)
    {
        if (otherShop.gold >= item.price) 
        {
            gold += item.price;
            otherShop.gold -= item.price;

            AddItem(item, otherShop);
            RemoveItem(item, this);

            RefreshDisplay();
            otherShop.RefreshDisplay();
            Debug.Log ("enough gold");

        }
        Debug.Log ("attempted");
    }

    void AddItem(Item itemToAdd, ShopScrollList shopList)
    {
        shopList.itemList.Add (itemToAdd);
    }

    private void RemoveItem(Item itemToRemove, ShopScrollList shopList)
    {
        for (int i = shopList.itemList.Count - 1; i >= 0; i--) 
        {
            if (shopList.itemList[i] == itemToRemove)
            {
                shopList.itemList.RemoveAt(i);
            }
        }
    }
}
Script 2 (From Tutorial Video)
using UnityEngine;
using System.Collections.Generic;

// A very simple object pooling class
public class SimpleObjectPool : MonoBehaviour
{
    // the prefab that this object pool returns instances of
    public GameObject prefab;
    // collection of currently inactive instances of the prefab
    private Stack<GameObject> inactiveInstances = new Stack<GameObject>();
    
    // Returns an instance of the prefab
    public GameObject GetObject() 
    {
        GameObject spawnedGameObject;
        
        // if there is an inactive instance of the prefab ready to return, return that
        if (inactiveInstances.Count > 0) 
        {
            // remove the instance from teh collection of inactive instances
            spawnedGameObject = inactiveInstances.Pop();
        }
        // otherwise, create a new instance
        else 
        {
            spawnedGameObject = (GameObject)GameObject.Instantiate(prefab);
            
            // add the PooledObject component to the prefab so we know it came from this pool
            PooledObject pooledObject = spawnedGameObject.AddComponent<PooledObject>();
            pooledObject.pool = this;
        }
        
        // put the instance in the root of the scene and enable it
        spawnedGameObject.transform.SetParent(null);
        spawnedGameObject.SetActive(true);
        
        // return a reference to the instance
        return spawnedGameObject;
    }
    
    // Return an instance of the prefab to the pool
    public void ReturnObject(GameObject toReturn) 
    {
        PooledObject pooledObject = toReturn.GetComponent<PooledObject>();
        
        // if the instance came from this pool, return it to the pool
        if(pooledObject != null && pooledObject.pool == this)
        {
            // make the instance a child of this and disable it
            toReturn.transform.SetParent(transform);
            toReturn.SetActive(false);
            
            // add the instance to the collection of inactive instances
            inactiveInstances.Push(toReturn);
        }
        // otherwise, just destroy it
        else
        {
            Debug.LogWarning(toReturn.name + " was returned to a pool it wasn't spawned from! Destroying.");
            Destroy(toReturn);
        }
    }
}

// a component that simply identifies the pool that a GameObject came from
public class PooledObject : MonoBehaviour
{
    public SimpleObjectPool pool;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
/
/ "BRO TIGER", 2017-09-12
@BRO_TIGER

I understand what's the matter! I disabled and enabled the shop canvas in the button settings on three buttons, and probably the canvas renderer in prefabs could not show sprites, labels, etc., a stupid mistake turned out)

A
Alex Maximovich, 2017-09-12
@flexer1992

All right. In the Start() method, the RefreshDisplay() method is called, which fills the list with the necessary items. If the monobehic is not active at the start of the game, then the Start method will not be called for it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question