Open Domiii opened 8 years ago
Do you mean copying in studio or using Clone()? I think Clone() is easier to get working in this case. Copying objects in studio serializes the objects, so values can only have a referent to other serialized objects. Fixing this is not easy and wouldn't work in all cases. I think we could probably do something to fix this though, just wanted to give some background on why it doesn't work.
@DarraghGriffin Doesn't matter how it is cloned/copied (be it in editor or via code), the reference is always 100% reset. It's really frustrating :)
As already explained on scripthelpers here,
ObjectValue
references are reset whenever an object is cloned. Here is why that is really bad:I am a big fan of Unity's
Prefab
andGameObject
model. Basically how it works is that you can assignGameObject
s orPrefab
s (which are GameObjects that are not in the world, but fully prepared to be spawned whenever needed, such as bullets) to properties on scripts, and scripts can then use those properties just like (in theory) you could do that withObjectValue
instances.For example - Here is how I would want to create a turret in Roblox:
This has a range of advantages. Most importantly: One script can support any amount of bullets without requiring any changes to the code, even during run-time!
However, in reality, when I clone my turret, I have to re-assign all
ObjectValue
s manually. That is especially annoying when there are many differentObjectValue
s on the same object, and makes run-time changes impossible.What's worse is that
ObjectValue
cannot be used at all with any object that is spawned/created by script (because it would immediately be invalidated). For example, right now, I am working on a simple pick-up system where tens of pick-ups are randomly spawned on the map, but different pick-up "templates" are supposed to contain different items/objects, with different scripts/effects.I want to avoid hard-coding of the referenced objects at any price. I can imagine several work-arounds, including:
My question is two-fold:
PS: (Not sure if anyone cares, but...) Unity is rather smart about this. When copying, all
Prefab
and non-localGameObject
references are kept as-is. However, if you reference an object that is a descendant of the object being copied, it will update the reference to match the newly instantiated copy of the descendant instead.