how to instantiate different objects? how to change orientation on the script of a rigid body?

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();

currentRb.detectCollisions = true;
}
}

}

Here is how to spawn with collision

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)
    {
    }

}
1 Like

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?

by two different, do you mean spawn two different at the same or spawn only one but it changes between the two objects?

1 Like

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)
    {
    }
}
1 Like

I mean spawning two different game objects like a plate and a cup for example

If i want to do this where do I add the other object with name test2 for example would I just change public GameObject[ ] test1, test2;

or more like
public GameObject[ ] test1,
public GameObject[ ] test2;
?

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)
    {
    }
}
1 Like

test1clone.transform.rotation = new Vector3(0,0,0);

i’m getting an error on this line

Do you know how I can fix this?

Try this
test1clone.transform.Rotate(Vector3.right);

I tried to run this code, but now when i press d nothing happens

no errors? no null refs?

none that I saw