Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Change the rotation of a prefab after instantiating

Discussion in 'Scripting' started by inicosia99, Nov 27, 2020.

  1. inicosia99

    inicosia99

    Joined:
    Oct 15, 2019
    Posts:
    90
    Hi everyone, I'm wondering if it is possible to change the rotation of prefab already spawned.
    thank you
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Sure, you just need a reference to the object. If the rotation is triggered by some external action, like a collision or a raycast, then it will usually provide you with the reference. Otherwise Instantiate also returns the reference to the newly instantiated object, so you can save it for later use.
     
    Terraya likes this.
  3. inicosia99

    inicosia99

    Joined:
    Oct 15, 2019
    Posts:
    90
    yeah but at the satrt of the game I spawn 100 objects, depending if I type right or left I need to rotate all the spawned objects? do you have an idea? thanks
     
  4. Siccity

    Siccity

    Joined:
    Dec 7, 2013
    Posts:
    255
    something like this

    Code (CSharp):
    1. for (int i = 0; i < 100; i++) {
    2.   GameObject go = Instantiate(prefab);
    3.   go.transform.position = Vector3.Zero;
    4.   go.transform.rotation = Quaternion.Euler(0,90,0);
    5. }
     
    bmvscary likes this.
  5. inicosia99

    inicosia99

    Joined:
    Oct 15, 2019
    Posts:
    90
    already tried but doesn't work
     
  6. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Doesn't work how?
    @Siccity's post would be the exact solution to rotate a GameObject after instantiating it.
     
  7. inicosia99

    inicosia99

    Joined:
    Oct 15, 2019
    Posts:
    90
    nope cause when I want to change the rotation of every gameobject doesnt work
     
  8. Siccity

    Siccity

    Joined:
    Dec 7, 2013
    Posts:
    255
    That code works exactly as it should. It spawns an object and resets its position to zero, and rotates it to a fixed (0,90,0) rotation.

    If you want to set another rotation you need to specify it.
     
  9. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    OP wants to first spawn 100 objects, then rotate all of them whenever he presses right or left.

    The solution is very similar tho. Just keep a List<GameObject> to which you add whenever you instantiate your objects. Then whenever you press your key, iterate over the list and set their rotations accordingly.
     
  10. inicosia99

    inicosia99

    Joined:
    Oct 15, 2019
    Posts:
    90
    yea but how to I keep a list??
     
  11. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Please clarify.
     
  12. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Code (CSharp):
    1. List<GameObject> allObjects = new List<GameObject>();
    2.  
    3. for (int i = 0; i < 100; i++) {
    4.   GameObject go = Instantiate(prefab);
    5.   addObject.Add(go);
    6. }
    Have the list somewhere as a member variable. Then just add the instantiated objects to it. When you want to set the rotations of your objects, do that for every object in that list with a loop.