- Home /
all instantiated walls face same way
i'm trying to make a part of procedural level generation with this
public class WallMaker : MonoBehaviour { public GameObject[] walls; void Start() { int rand = Random.Range(0, walls.Length); Instantiate (walls[rand], transform.position, Quaternion.identity); Destroy(gameObject); } }
i tried to parent the instantiated object by adding "transform.parent" with all manner of different capitalizations, into many places in the script but i don't know how to do it correctly. i need the spawned walls to face the center of the room (obviously) but i don't know what the best way to go about this is. do i really need to make different scripts or variables for walls facing different directions? or is there a way for the instantiated object to just inherit the orientation from the parent object?
i tried looking up how all the pieces of the code work and what you can do with them but i'm just not well versed in c# yet to understand this
Answer by henkehedstrom · Jan 07 at 03:19 PM
If I understand this correctly you have a bunch of wallmakers around in the level like my image that are already manually rotated towards the center. Then you could instantiate a wall at the wallmakers position and rotation to make the wall face the center. You only have to change Quaternion.Identity to transform.rotation then. Quaternion.Identiy means no rotation. https://docs.unity3d.com/ScriptReference/Quaternion-identity.html
So your code would look like this instead:
WallMaker : MonoBehaviour
{
public GameObject[] walls; void Start()
{
int rand = Random.Range(0, walls.Length);
Instantiate (walls[rand], transform.position, transform.rotation);
Destroy(gameObject);
}
}
Btw you say that it is obvious that the walls should face the center but I don't agree. I don't really know what you want to achieve so it would be good if you provided a picture.