Hi,
I’ve been banging my head over this for a good day now, I’m instantiating randomly from an array of prefabs, but everything I have personally tried so far doesn’t seem to work, the instantiate works fine, but I’d like to make the instantiated objects children of the parent object the script is attached to. Now, search results have offered up numerous ways of doing this, but all seem to deal with singular objects, I can’t seem to find anything that works for me when instantiating from an array ? Any pointers / code snippets ? The basic code so far.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class trackSegmentSpawn : MonoBehaviour {
[Header("Prefabs To Spawn References")]
public GameObject[] prefabsToSpawn;
[Header("Lane Width Reference")]
public float laneWidth = 1.5f;
private int randomInt;
void Start () {
Debug.Log("Parent gameObject name : " + gameObject.name);
spawnOnSegment ();
}
public void spawnOnSegment () {
// Lane position
float lanePosition = Random.Range(0, 3);
lanePosition = (lanePosition - 1) * laneWidth;
// Choose a random prefab from prefabsToSpawn array
randomInt = Random.Range(0, prefabsToSpawn.Length);
// Instantiate to Vector3 ( x = lanePosition ) ( y = 0 ) ( z = random between -5 and 5 )
Instantiate (prefabsToSpawn[randomInt], new Vector3(lanePosition, 0, Random.Range(-5f, 5f)), Quaternion.identity);
}
}
Single objects and from an array are exactly the same thing. Only difference is you’re picking an item from an array. So either you assign your Instantiated object to a gameobject variable and then use SetParent on the transform of that gameobject or you assign the parent in the Instantiate call as it shows
Instantiate (prefabsToSpawn[randomInt], new Vector3(lanePosition, 0, Random.Range(-5f, 5f)), Quaternion.identity);
becomes
Instantiate (prefabsToSpawn[randomInt], new Vector3(lanePosition, 0, Random.Range(-5f, 5f)), Quaternion.identity, transform); //Makes the object this script is attached to the parent of the Instantiated object.
1 Like
GameObject obj = Instantiate (prefabsToSpawn[randomInt], new Vector3(lanePosition, 0, Random.Range(-5f, 5f)), Quaternion.identity) as GameObjecty;
obj.transform.parent = transform.parent;
1 Like
Thanks, @Brathnann & @AcePilot10
@Brathnann Thank you for clarifying how to apply the transform property properly, I was really confused over the usage, trying transform.parent, transform gameObject, etc and not finding a workable solution.
@AcePilot10 Thanks also, seems this was solution offered by many of my searches, or at least variations on it, but I had some trouble understanding it in connection with my code.
EDIT : New code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class trackItemsSpawn : MonoBehaviour {
[Header("Prefab To Spawn")]
public GameObject[] prefabsToSpawn;
[Header("Prefab Y Offset Property")]
public float yOffset = 0.5f;
[Header("Prefab Spawn Properties")]
public float spawnFrom = -2; // Distance to spawn from
public float spawnTo = 4; // Distance to spawn to
public float minSpread = 2; // Minimum spread between spawned objects
public float maxSpread = 3; // Maximum spread between spawned objects
[Header("Spawn Limit")]
public int spawnMax = 3; // Maximum amount of times to spawn
[Header("Lane Width Reference")]
public float laneWidth = 1.5f;
private float zSpread;
private float parentZPos;
private int randomInt;
void Start() {
zSpread = Random.Range(minSpread, maxSpread);
parentZPos = gameObject.transform.position.z + (spawnFrom - zSpread - spawnTo);
spawnOnSegment();
}
public void spawnOnSegment () {
for (int timesToSpawn = 1; timesToSpawn <= spawnMax; timesToSpawn++) {
float lanePosition = Random.Range(0, 3);
lanePosition = (lanePosition - 1) * laneWidth;
// Choose a random prefab from prefabsToSpawn array
randomInt = Random.Range(0, prefabsToSpawn.Length);
Instantiate(prefabsToSpawn[randomInt], new Vector3(lanePosition, yOffset, parentZPos + zSpread + spawnTo), Quaternion.identity, transform);
parentZPos += zSpread;
zSpread = Random.Range(minSpread, maxSpread);
}
}
}