I’m instantiating a game object from a reference to a prefab, like this:
var starSystem : GameObject;
function OnTriggerExit2D (other: Collider2D)
{
var newSystem : GameObject = Instantiate(starSystem, newSystemPosition, Quaternion.identity);
}
The first time the function gets called, it creates a clone of the prefab that starSystem refers to, but every subsequent time, it creates a clone of the previous clone, not a clone of the unmodified prefab in the project. I just want to instantiate from the prefab, not from a clone; I definitely haven’t changed the reference from a reference to the prefab, to a reference to the last clone, in a script anywhere. What’s going on?
Edit – It’s probably of note that the script which instantiates the new clone is a component in an instance of the prefab it’s trying to instantiate.
It sounds like you haven’t built your prefab correctly and it refers to an existing object in the scene rather than an actual prefab as you expect. I have seen this behaviour before but can’t remember exactly how it happened.
I ‘think’ this happens if you have a prefab which contains another prefab But you passed it an instance of an object in the scene rather than the prefab itself.
As for my suggestion, try remaking the prefab and make sure it copies into a blank scene with no issues.
I have never see this behavior … really strange. Maybe there was some error on your script when you use the starSystem var, or maybe there was some statics var.
If you change the material, check if you change the “sharedMaterial” or the “material” property …
That the normal behaviour and is intended! Self-references are converted after cloning. In most cases that’s more useful than a problem.
Imagine a prefab like this:
Gun-Prefab
C Gun script
C ...
- Model
- bones
- BulletSpawnPoint
Now imagine the Gun script has a reference to Model (for controlling animation) and a reference to BulletSpawnPoint. When you instantiate the Gun prefab you want those two references to point to the instantiated gameobjects and not to the objects in the prefab.
There’s no way to change this behaviour. One way around this is a two prefab approach.
//Holder.js
var starSystem : GameObject; // your prefab
and
// Your Starsystem script which is part of the prefab:
var holder : Holder; // the Holder prefab.
function OnTriggerExit2D (other: Collider2D)
{
var newSystem : GameObject = Instantiate(holder.starSystem, newSystemPosition, Quaternion.identity);
}
So one prefab hold the reference to the other. That way the prefab references stay intact since they are not self references.
edit
Another way is to reset the reference when you have instantiated it. It’s way easier when you clone the script itself unless it’s not at the prefabs root.
// StarSystem.js
var starSystem : StarSystem;
function OnTriggerExit2D (other: Collider2D)
{
var newSystem : StarSystem = Instantiate(starSystem, newSystemPosition, Quaternion.identity);
newSystem.starSystem = starSystem; // reset the reference.
// newSystem.name = name; // also quite common to get rid of the "(clone)" in the name.
}
This of course only works if the first instance has a reference to the prefab. The problem here is if you instantiate another system somewhere else and you don’t think about that, the new system will again have a selfreference and all subsequent created sytems will use this one as source.
The first approach is more robust the second might be a bit more flexible but has the risk of losing the prefab when not cloned the right way.