- Home /
Attribute inheritance problem when using Instantiate
Ive got a prefab (a space ship) occasionally Instantiating another of the same kind of prefab (another space ship) from within the instantiator's script. When I do this the new ship that's spawned does not inherit all of the attributes that I assigned in the editor for prefabs of this type, but the attributes of the ship that spawned it. Is there a simple way to tell it to not adopt all of the attributes of its instantiator or must I reasign the variables one-by-one after I've instantiated it.
Also, the variable that's being pointed to Instantiate to tell it what prefab to instantiate was set in the editor and is a reference to the prefab. I'm not (at least knowingly) pointing Instantiate to the transform of the object that's doing the instantiating.
Thanks for any help you can provide,
Dave
$$anonymous$$y first thoughts were squashed by your second paragraph. So I suggest posting some code.
yeah i think that ultimately there must be a problem with instantiating this prefab directly from another instance of the same prefab. Short of anybody knowing exactly why this is a problem ive just started instantiating the prefab from a different class
You've got it. Instantiate from another class or use a manager class to store the prefab reference like i showed in my answer.
Answer by Bunny83 · Jan 30, 2011 at 12:34 PM
First of all, it's not an attribute. At least in C# an attribute is a special class that can be attached to another class. See Attribute. You talk about member variables of your class.
Anyway, this behaviour is a bit hard to understand. I don't will go into detail now. Fact is if you have a prefab with a script that holds a reference to itself, this reference will be replaced with your object instance at creation time. Just avoid self-pointing references. Use eg. a singleton manager for the reference.
More details on this issue in the Unity Forum
Here a singleton manager example in JS (i don't use JS but i've writting this already for somebody else ;) )
// The simplest singleton without checking // scriptfile is named: "PrefabManager"
static var manager : PrefabManager = null;
var spaceShipPrefab : GameObject;
function Awake() { manager = this; }
Place this script on an empty GameObject and assign your prefabs to your public variables.
In any other script (even in a script on your spaceship) you can do:
Instantiate(PrefabManager.manager.spaceShipPrefab);
to create a new instance.