N
N
nano_e_t_42019-07-07 18:23:41
Game development
nano_e_t_4, 2019-07-07 18:23:41

How to access the fields of a disabled object?

Hello everyone
I want to get access to the field of the object (inventory), which I turn off at startup

public GameObject inventoryBlank;
    void Start()
    {
        inventory.SetActive(false);
    }

inventory, respectively, I link through the editor
, but when I try to access the object in the distance, I get an error:
Object reference not set to an instance of an object

but when the inventory is enabled, there is access to variables
on the child objects of the inventory (to which access is needed) there is no canvas, so the method through
MeshRenderer render = gameObject.GetComponentInChildren<MeshRenderer>();
 render.enabled = false;

also did not take off (
please tell me, who knows how to access the variables of the turned off gameobject

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Gaydak, 2019-07-08
@nano_e_t_4

please give an example of where exactly the access to the inventory is and what exactly is there in the inventory for the code.
and partly Konstantin Kitmanov is mistaken. and freeExec is partly true to you. that when you refer to an object, you will have access to fields and methods.
and here's what it would be important for you to understand, NO SEARCH works on DISABLED objects.
GetComponentInChildren is also a SEARCH. and everything like him will not be able to find anything on turned off objects.
maybe in your inventory you don’t just get a value, but something like

inventory.GetItemInfo(0);
///
void ItemInfo(int itemIndex)
{
something.GetComponent<T>();
}

and this will not work out, so do not be lazy to apply more code)) although the entire project is also not worth it. hmm, it's not easy to guess))

K
Konstantin Kitmanov, 2019-07-07
@k12th

Actually, no way. The only way out is to turn it off somehow differently, disable the renderer, disable handlers, skip Update, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question