Search Unity

Changing prefabs name not casting correct variables

Discussion in 'Scripting' started by All_American, Jan 18, 2020.

  1. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    I am instantiating an object then attaching an instantiated UI panel to it and then changing the panels name and sending data to it.

    If I instantiate another panel to another object and send the data it is not the data for the object but the first object.

    Here's how I am doing it.

    Code (CSharp):
    1. GameObject NewCanvas =  Instantiate(Model_Data_Canvas_Prefab, new Vector3(0, 1000, 0), Quaternion.identity);
    2.  
    3.             string newName = "Model_Data_Canvas" + "_" + _filename;
    4.  
    5.             NewCanvas.name = newName;
    6.  
    7.             NewCanvas.transform.SetParent(_Modeldata.transform, true);
    8.  
    9.             _meshData = GameObject.Find(newName).GetComponent<Mesh_Data>();
    10.             if (_Modeldata.name == _filename)
    11.             {
    12.                 Mesh_Data.stored_GUID = _guid;
    13.                 Mesh_Data.stored_ElementID = _elementid;
    14.                 Mesh_Data.stored_Name = _name;
    15.                 Mesh_Data.stored_AssetTag = _assettag;
    16.                 Mesh_Data.stored_Type = _type;
    17.                 Mesh_Data.stored_Layer = _layer;
    18.                 Mesh_Data.stored_Location = _location;
    19.             }
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    May i ask why you are using Find() to find an object you just created a few lines above? You literally have the reference right there, and Find (and all its variations) are very slow and should always be prevented outside of rapid prototyping.
    Not to mention that you do not use _meshData for anything at all afterwards.

    As for your problem, i'm pretty sure your variables, such as stored_GUID, are static, as you are accessing them directly from the type Mesh_Data. Static variables are bound to the type itself, not the instance. Thus if you change it, you change the one and only representation of those values that is in memory.
    You probably want these variables to be public (non-static) instead, and then change the variable values of the found _meshData instance (which you can get by writing NewCanvas.GetComponent..instead of using Find).
    So it would be _meshData.stored_GUID = xyz.
     
  3. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    I am accessing the script "Mesh_Data" it doesn't work any other way.

    Here is the Mesh_Data script side

    Code (CSharp):
    1. public class Mesh_Data : MonoBehaviour
    2. {
    3.     public static string stored_GUID;
    4.     public static int stored_ElementID;
    5.     public static string stored_Name;
    6.     public static string stored_AssetTag;
    7.     public static string stored_Type;
    8.     public static string stored_Layer;
    9.     public static string stored_Location;
     
  4. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    If not static I get a non-static error..
     
  5. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    I am doing the same thing with another script and it’s working perfectly. This one is giving me problems though.
     
  6. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    This does the same thing.

    Code (CSharp):
    1. GameObject NewCanvas =  Instantiate(Model_Data_Canvas_Prefab, new Vector3(0, 1000, 0), Quaternion.identity);
    2.  
    3.             string newName = "Model_Data_Canvas" + "_" + _filename;
    4.  
    5.             NewCanvas.name = newName;
    6.  
    7.             NewCanvas.transform.SetParent(_Modeldata.transform, true);
    8.  
    9.             NewCanvas.GetComponent<Mesh_Data>();
    10.  
    11.             if (_Modeldata.name == _filename)
    12.             {
    13.                 Mesh_Data.stored_GUID = _guid;
    14.                 Mesh_Data.stored_ElementID = _elementid;
    15.                 Mesh_Data.stored_Name = _name;
    16.                 Mesh_Data.stored_AssetTag = _assettag;
    17.                 Mesh_Data.stored_Type = _type;
    18.                 Mesh_Data.stored_Layer = _layer;
    19.                 Mesh_Data.stored_Location = _location;
    20.             }
    I get this

    Code (CSharp):
    1. cannot be accessed with an instance reference; qualify it with a type name instead
    If I do this

    Code (CSharp):
    1. _meshData = NewCanvas.GetComponent<Mesh_Data>();
    2.  
    3.             if (_Modeldata.name == _filename)
    4.             {
    5.                 _meshData.stored_GUID = _guid;
    6.                 _meshData.stored_ElementID = _elementid;
    7.                 _meshData.stored_Name = _name;
    8.                 _meshData.stored_AssetTag = _assettag;
    9.                 _meshData.stored_Type = _type;
    10.                 _meshData.stored_Location = _location;
    11.             }
     
  7. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,326
    All the fields in your Mesh_Data class contain the static modifier.

    The static modifier basically means that the field will only have a single value shared by all objects, instead of each component having their own individual value for the field. When using the static keyword you have to use the type instead of a specific component returned by GetComponent to access the fields.

    So to get rid of the error you just need to remove the static keyword from all the Mesh_Data fields. Each component would then have their own Mesh_Data values, which I think is what you want.
     
    All_American likes this.
  8. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    As i said before, and as SisusCo also stated, if something is static, then it is bound to the type, not any instance of that type. Thus only one representation of these variables exists in memory. There can only be one value for these variables. It simply does not exist more than once. So to speak, all instances share this one value. They do not have their own values.

    If you remove static, you then need to reference the instance instead of the type. So instead of Mesh_Data.variableName you would access the variable through _meshData.variableName. So do both, not just one. Change it to non-static and use the instance reference. This should fix your error.

    Honestly tho, you should definitely look into the basics of object oriented programming, since you have a major misunderstanding / knowledge gap rhere. After you understand how objects work, static (and why your approach does not work) should become more clear to you as well.

    Edit: Also please refrain from multi-posting as it gets messy really quickly. There is an edit function you can use to your hearts content, so you dont need to post 4 times in a row.
     
    Last edited: Jan 18, 2020
  9. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    Then why is it working for another set of variables the same way I’m trying do this that keeps its original data and not all the same data.

    if I remove the static from it and use the _meshData it doesn’t work that way either.
     
    Last edited: Jan 18, 2020
  10. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    Thanks @SisusCo for explaining it without the extra bs.

    I tried that though. But I’ll over look it again and see if I missed something.
     
  11. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,326
    Happy to help :)

    Removing the static modifiers should be the right solution here, so if you're still seeing errors after removing them the problem should lie somewhere else.
     
  12. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    It works. Not sure why it wouldn't last night...

    But I am doing this same thing with another set of data and it works. Could it be working because I attach the script to the object after the object is instantiated?
     
  13. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    What the...
    Sorry for not only solving your problem with my very first post, but also telling you what you may look into if these kind of things trouble you - or telling you that multiposting is against the rules of the forum (or rather all forums). Sorry for all my bs, others have been very grateful for it. I will refrain from ever bothering (helping) you in the future lol.