![]() I expect a prefab to always instance a fresh copy, not a copy of a copy. I can easily store a reference to an instance from a script, I don't need some elaborate reference passing to try and solve this. This creates a black-hole of debugging time. I'd like to say this is not the expected result from a user point of view. The dictionary would be created on startup and used globally. If this works, I would likely go one step further and make a globally-accessible static dictionary (singleton), so I don't have to do anything in Awake(). Any script that needs to make instances of these references could stor a reference to "Globals" on Awake() then instance with that reference instead of the local script drag-and-drop reference. I already have a "Globals" game object I use for other stuff, this is in the scene before the game starts and I suppose I could place a script on there that holds references to the prefab. Work-around? (still need to try this tomorrow): However, you might have given me a workaround I can use, it just seems a bit insane. Making a copy of the copy, with the changed state, really messes things I can't use that work-around that because these objects are placed by a user. My problem is that this game object has a starting state in the prefab, and once added to the scene, will change. It only seems to happen if the gameobject is instanced during the game play. I have a theory that if I can somehow break the prefab connection before Unity does step 2.), that would fix The reference is good if the gameobject is placed in the scene then the game is started. This makes sense because at run time they are essentially clones of each other, and instantiating one over the other makes no difference, but if I store the variable myPrefab in another script and try to instantiate it later, the changed reference means I'm trying to instantiate an object that was destroyed - which is against the rules.ġ.) Object 1 stores a reference to its own prefab.Ģ.) At run time that reference changes from the prefab in the Project folder to the in-scene instance of the object.Ģ.) Object 2 copies that reference and destroys object 1.ģ.) When Object 2 tries to instantiate using that reference, it gets a NullReferenceException because the object no longer exists. However, at run time, the variable points not at the prefab in the Project folder, but to the actua in-game GameObject. This reference is copied into a holder var before the object is destroyed. The Item also had a variable, myPrefab, that pointed to the prefab that it is a copy of. ![]() I have a script that goes on an Item object, and when the player collides with it the Item is added to the Inventory and the object is destroyed. For me, it's a slightly different situation, though. I have this same issue, and I really want to get it fixed. In the mean-time, I'll start stripping back my code to see if I can uncover it. ![]() Something is triggering the change, but I don't know where to start my search. It does not set this reference anywhere in the code. My goal is to have this game object make copies of itself using the GUI Button. This is a pretty simple setup, but kind of hard to explain. When I drag the original prefab in to the scene, everything looks fine, but the when I use the UI to make an instance/clone, and look at the new clone in the inspector, the reference is no longer to the prefab, it is a self-reference to the clone in the scene. If I want an instance (clone) of the prefab to instanciate another clone of its prefab (meaning the reference in the prefab's script is referencing itself), I run in to a problem. The prefab to instantiate is a drag dropped reference on a transform parameter in the script. When the button is clicked it instanciates a prefab. I have a script on a prefab which displays a GUI button. ![]()
0 Comments
Leave a Reply. |