Unwanted, Unpredictable Values for Transform

I am trying to instantiate a pickup prefab within a room when an enemy is killed. I have tried to set it to where the prefab is instantiated at the position of the enemy. Simple, right?..

…not exactly. I tried the easy, obvious, really-should-be-correct position for the instantiated object (transform.position), but that nearly always had the item instantiate out of the room. (Just to let you know, the object which instantiates the prefab is within the room, is a child of the enemy, and is exactly where I want). Then I thought that I would try to use Mathf.Clamp() to limit where the prefab would instantiate. That worked partly, with the x and z values within proper range, but the y values were still off (it either wasn’t in the room or out of reach about two-thirds of the time). I thought that I would let the x and z values be, and set the y value to a constant float. This is functional; the prefab doesn’t usually instantiate with the proper y value, but is within reach two-thirds of the time, with the other one-third being under the room.

Does anyone have an idea of how to fix this perplexing problem? The code that I am using to instantiate the prefab (which is of a key, btw) is shown below.

private var key : GameObject;
var xMax : float;
var xMin : float;
var yPosit : float;
var zMax : float;
var zMin : float;

function Start () {
	key = transform.gameObject.parent.MarshmellowManDamage.key;
}

function OnDestroy () {
	var obj = Instantiate(key,Vector3(Mathf.Clamp(transform.position.x,xMin,xMax),Mathf.Clamp(transform.position.y,yPosit,yPosit),Mathf.Clamp(transform.position.z,zMin,zMax)),Quaternion.identity);	
	obj.transform.position.y = yPosit;
} 

================================================================

Edit : I have changed the scaling of the prefab and the enemy, but the enemy now either flies around the room, or just drops and the keys are still a problem.

Maybe i’ve missed it somewhere in the 27+ comments, but nobody mentioned this line:

key = transform.gameObject.parent.MarshmellowManDamage.key;

That doesn’t make any sense…

  1. a GameObject doesn’t have a parent, only the transform component has one.
  2. Even if parent would exist and would return something meaningful (usually a Transform), MarshmellowManDamage wouldn’t be a property / variable of transform. You have to use GetComponent if you want to access a custom script component.
  3. If you put a #pragma strict at the top the compiler won’t even compile your code.
  4. If i run your code without pragma strict, it obviously throws a null-ref-exception at the line in Start.

So if the “MarshmellowManDamage” script is attached to the parent object, you would do something like that:

key = transform.parent.GetComponent(MarshmellowManDamage).key;

So i have a script that looks like this and works fine:

// UnityScript
private var key : GameObject;

function Start () {
    key = transform.parent.GetComponent(MarshmellowManDamage).key;
}

function OnDestroy () {
    Instantiate(key, transform.position, Quaternion.identity);   
} 

Another final note: It seems it’s not a good idea to use Instantiate in OnDestroy or OnDisable and test in the editor. This will instantiate the object into the edit-scene when you exit the game. That’s a really strange behaviour. It does the same in a C# script. This will leak the object into the scene which is an absolutely “NoGo”. It shouldn’t be possible to corrupt the edit scene from the runtime.

I still use 3.5.0f1. Maybe this is already fixed…