how to Instantiate at a set postion and SET object rotation?

Hi, long story short, I’ve been using unity on and off for a few years and stupidly enough keep finding myself coming back to have another shot at a failed project, every time I come back I usually have forgotten the formula to have a specific function happen in this instance it is that I wish to instantiate a Random object at a Random position with a little variation to the landing postion and have it rotate to a random rotation.

so the code I have is as follows:

void GenerateObject()
{
    randTimeNum = Random.Range(minSpawnTime, maxSpawnTime);
    randObjectNum = Random.Range(0, spawnableObjects.Length);
    randDiffSizeX = Random.Range(minDifferSizeX, maxDifferSizeX);
    randDiffSizeZ = Random.Range(minDifferSizeZ, maxDifferSizeZ);
    randSpawnLocation = Random.Range(0, spawnLocations.Length);
    newTrans = spawnLocations[randSpawnLocation].transform;
    Debug.Log("RandTimeNum is now: <color=red>" + randTimeNum + "</color>.");
    Debug.Log("RandObjectNum is now: <color=red>" + randObjectNum + "</color>. Which is: <color=green>" + spawnableObjects[randObjectNum].name + "</color>.");
    newObj = Instantiate(spawnableObjects[randObjectNum], new Vector3(newTrans.position.x + randDiffSizeX, 0, newTrans.position.z + randDiffSizeZ));
    newObj.transform.Rotate();
}

The issue I am having is on the last TWO lines of this function… for one I have forgotten the formula for setting my newly spawned object at a specified position + the little variation to stop a stack of objects being piled very neatly ontop of each other (and when I try and google search this it is either very poorly explained, or explained in a way that you have to figure out the rest of the forumla if you wish to add a variation, ie: https://forum.unity.com/threads/instantiate-object-at-another-object.285221/)

The other one is when I try and search ‘how to SET the rotation of an object’ all that returns is, Lets show you how to rotate an object by 1 every frame, or by 1 every second… I do not wish to have a spinning object! I wish to set the objects rotation to a random digit upon it spawning and physics to take over the rest as it hits the ground to have variation.

Thank you If answered I will bookmark this post so I can forever refer back to it as googling this question every time I return just leaves me with headaches and pulling my hair out as it returns a… “oh its simple you just use transform.position and it will work!” which for my case no, it will not simply work like that…

To change the position and rotation after you have spawned an object, you can simply access the spawned gameobject’s transform and change the transform.position and transform.rotation. In this code example, I’m changing the rotation’s Y value to a random of 0-360.

To try this out, put this script on an empty gameobject and drag a prefab or two into the spawnableObjects, drag a few spawnlocations in and then right click the script while the game is running and hit the “Spawn!” button in the menu-popup. It will spawn X many objects depending on what you set the m_amountToSpawn to.

I hope this helps you in what you’re trying to achieve!

public class SpawnObjects : MonoBehaviour
{
    [SerializeField] GameObject[] spawnableObjects = new GameObject[0];
    [SerializeField] GameObject[] spawnLocations = new GameObject[0];
    [SerializeField] float minSpawnTime = 0.5f, maxSpawnTime = 1.5f, minDifferSizeX = 1.5f, maxDifferSizeX = 3.0f, minDifferSizeZ = 1.5f, maxDifferSizeZ = 3.0f;

    [SerializeField] [Range(0,100)] int m_amountToSpawn = 10;
    [ContextMenu("Spawn!")]
    void Spawn()
    {
        for (int i = 0; i < m_amountToSpawn; i++)
        {
            GenerateObject();
        }
    }
    void GenerateObject()
    {
        if(spawnableObjects.Length < 1 || spawnLocations.Length < 1)
        {
            Debug.Log("Spawnable objects or spawnlocations missing!");
            return;
        }

        float randTimeNum = Random.Range(minSpawnTime, maxSpawnTime);
        int randObjectNum = Random.Range(0, spawnableObjects.Length);
        float randDiffSizeX = Random.Range(minDifferSizeX, maxDifferSizeX);
        float randDiffSizeZ = Random.Range(minDifferSizeZ, maxDifferSizeZ);
        Transform newTrans = spawnLocations[Random.Range(0, spawnLocations.Length)].transform;
        Debug.Log("RandTimeNum is now: <color=red>" + randTimeNum + "</color>.");
        Debug.Log("RandObjectNum is now: <color=red>" + randObjectNum + "</color>. Which is: <color=green>" + spawnableObjects[randObjectNum].name + "</color>.");

        GameObject newObj = Instantiate(spawnableObjects[randObjectNum]);
        if(newObj)
        {
            newObj.transform.position = new Vector3(newTrans.position.x + randDiffSizeX, newTrans.position.y, newTrans.position.z + randDiffSizeZ);
            newObj.transform.rotation = Quaternion.Euler(new Vector3(0, Random.Range(0, 360), 0));
        }
    }
}