Hello I’m trying to instantiate 2 objects after a collision, right now I have it set up so that when I press “d” 1 object instantiates, but for the purpose of the game it makes more sense that the instantiation happens after a collision. Right now I only know how to instantiate a single object to which I need to change the orientation of and can’t figure out how to do this in the script because i’m new to c#.
current script is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class instantiateprefab : MonoBehaviour
{
public GameObject test1;
GameObject test1clone;
void Update()
{
if (Input.GetKeyDown(“d”))
for (int i = 0; i < 1; i++)
{
Rigidbody currentRb;
GameObject test1clone = Instantiate(test1, transform.position, Quaternion.identity) as GameObject;
currentRb = test1clone.AddComponent();
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class instantiateprefab : MonoBehaviour
{
public GameObject test1;
GameObject test1clone;
void Update()
{
if (Input.GetKeyDown("d"))
{
SpawnObject();
}
}
void SpawnObject()
{
for (int i = 0; i < 1; i++) //this for loop is not needed, < 1, means it only runs once. but if you later want to spawn 5, then it's good to have it.
{
Rigidbody currentRb;
GameObject test1clone = Instantiate(test1, transform.position, Quaternion.identity) as GameObject;
currentRb = test1clone.AddComponent<Rigidbody>();
//change the orientation here
test1clone.transform.rotation = new Vector3(0,0,0); // you'll need to play around with this part.
currentRb.detectCollisions = true;
}
}
void OnCollisionEnter(Collision collision)
{
Debug.Log("I hit : " + collision.gameObject.name);
SpawnObject();
}
void OnCollisionStay(Collision collision)
{
}
void OnCollisionExit(Collision collision)
{
}
}
Thanks, this was really helpful. This for loop was added because my plan was to add 2 different objects at the same time after the collision, can you explain the correct way to do this then?
updated to code to spawn multiple gameObject at the same time
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class instantiateprefab : MonoBehaviour
{
public GameObject[] test1; //this is now an array, so how ever many object you add, it will spawn them all.
void Update()
{
if (Input.GetKeyDown("d"))
{
SpawnObject();
}
}
void SpawnObject()
{
for (int i = 0; i < test1.Length; i++) //this was updated to use the array
{
Rigidbody currentRb;
GameObject test1clone = Instantiate(test1[i], transform.position, Quaternion.identity) as GameObject; //this line was changed to use the array
currentRb = test1clone.AddComponent<Rigidbody>();
//change the orientation here
test1clone.transform.rotation = new Vector3(0,0,0);
currentRb.detectCollisions = true;
}
}
void OnCollisionEnter(Collision collision)
{
Debug.Log("I hit : " + collision.gameObject.name);
SpawnObject();
}
void OnCollisionStay(Collision collision)
{
}
void OnCollisionExit(Collision collision)
{
}
}
The question is really how are going to deside witch gameObject to spawn?
I would do this for randomly spawning them
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class instantiateprefab : MonoBehaviour
{
public GameObject[] test1; //this is now an array, so how ever many object you add, it will spawn them all.
public GameObject[] test2;
void Update()
{
if (Input.GetKeyDown("d"))
{
SpawnObject(test1);
}
}
void SpawnObject(GameObject[] SpawnList)
{
for (int i = 0; i < SpawnList.Length; i++) //this was updated to use the array
{
Rigidbody currentRb;
GameObject test1clone = Instantiate(SpawnList[i], transform.position, Quaternion.identity) as GameObject; //this line was changed to use the array
currentRb = test1clone.AddComponent<Rigidbody>();
//change the orientation here
test1clone.transform.rotation = new Vector3(0,0,0);
currentRb.detectCollisions = true;
}
}
void OnCollisionEnter(Collision collision)
{
Debug.Log("I hit : " + collision.gameObject.name);
int rand = Random.Range(0, 10);
if(rand > 4)
{
SpawnObject(test1);
}else
{
SpawnObject(test2);
}
}
void OnCollisionStay(Collision collision)
{
}
void OnCollisionExit(Collision collision)
{
}
}