Issues with my parents.

Hi,

Not the kind of issues Freud talks about. The attached project contains a scene where a script creates multiple instances of a prefab. The prefab has a script that triggers the linking to another object on a collision trigger event. The problem is that when the prefab instances are spawned, several of them are immediately linked to the target parent object.

It doesn’t happen every time the scene is played. Maybe once every three times.

I need it so there are absolutely no prefabs linked to the target parent. Keeping in mind there will be other types of children linked to the target parent, meaning I can’t just blanket unlink all children.

Anyone have any ideas?

Cheers,
Dan.

627079–22350–$parentingIssues.unitypackage (18.7 KB)

Use an if loop to check. Perhaps use a Start function like.

var target : Transform;
function Start()
{
    if (target != null)
    {
        target = null;
    }
}

Note: i didn’t download your file to see. So i might be wrong. But something like this i guess.

Thanks Zine92, just tried that and still no luck. Has anyone else had issues with collision triggers used to parent to an object in a scene with multiple instances of that object? I’ve had a search through the forums and have had no luck tracking down a solution.

hmmm, I’ve been trying to solve this for three nights now and still no luck. Admittedly I’m still very much learning so have no idea what I’m doing. zine92’s suggestion may have done the trick, but I’m not too sure if I fully understand how to implement it (I thought I did, but now I’m not so sure.)

I have this code on an empty game object, which works fine:

public final var MAX_SPACEMEN = 200 ;
public final var AREA_SPAWNED_IN = 200;

function Start(){
	CreateInitialSpacemen();
	SpawnPrefab("p_player", 2,2,0 );
	SpawnPrefab("p_camera",0,0,-10);
}

		static function SpawnPrefab(prefabName : String, posX, posY, posZ ) {
		var localPath : String = "Assets/GalacticRescue/Prefabs/" + prefabName + ".prefab";
        var obj = (AssetDatabase.LoadAssetAtPath(localPath, GameObject));
	
		Instantiate (obj, Vector3(posX, posY, posZ), Quaternion.identity);	

}

function CreateInitialSpacemen(){
		for(var i=0; i<MAX_SPACEMEN; i++){
		
			SpawnGameAssetAtRandomXY("p_spaceMan", 10);
		}
}

function SpawnGameAssetAtRandomXY(assetName, rangeValue : float){
				var gameAsset : String = "Assets/GalacticRescue/Prefabs/" + assetName + ".prefab";
				var loadedGameAsset = (AssetDatabase.LoadAssetAtPath(gameAsset, GameObject));
				Instantiate (loadedGameAsset, Vector3(Random.Range(-rangeValue, rangeValue),(Random.Range(0, -AREA_SPAWNED_IN)),0), Quaternion.identity);
				
}

And I have this code running on the spawned game asset (p_spaceMan):

function OnTriggerEnter (other:Collider) {
  		this.transform.parent = (GameObject.Find("GR_ShipRotate").transform);
		Debug.Log ("Object Linked", gameObject);
}

But when I run the scene, a random number of p_spaceMan assets are immediately linked to GR_ShipRotate, even though they’re no where near the parent collider.

Anyone able to help? I’m pulling my hair out.