Getting C# object reference in Unity3D -


i want c# object in unity clicking on it. made ray , works properly. problem in copying object. i'm making inventory system. of collectable items have item scripts variables name or id. getting c# object of script gameobject i'm using

itemlist.add (hitinfo.transform.gameobject.getcomponent<item> ()); // adding c# object c# list 

and destroy gameobject (maybe it's problem), when try print itemlist[0].name it's not work because of: "nullreferenceexception: object reference not set instance of object" it's caused destroing object? or should copy reference of object in other way (by not using itemlist.add (hitinfo.transform.gameobject.getcomponent<item> ()))?

edit://exception appearce if don't destroy gameobject. edit2://when start game , check itemlist.count; (it should show number of elements in list think) it's 15 not 0 , don't know why.

from understanding, code posted getting "item" component object click on. component instance must have single object associated with. when delete object, deleting "item" component on object.

what should have data model class item field on component. class contain whatever data (name, required inventory. instead of adding item component list add data model. reference model class persist when gameobject deleted.

public class item : monobehaviour {     [system.serializable]     public itemmodel model; }  public class itemmodel {     public string name;     public int id; } 

get this:

gameobject.getcomponent<item>().model 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -