Object Rotation When Instantiating in C# - Help!

Hello, im trying to rotate an object when i instantiate it but for some reason nothing i do seems to work… This is the code im useing to instantiate my item all i want to do is Rotate it when it gets instantiated , The code below is without any attempt to apply rotations. - Thanks in advance if anyone can help me out :slight_smile:

GameObject TorchFireParticles = (GameObject)Instantiate(Resources.Load("TorchFire"), transform.position = voxelInfo.chunk.VoxelIndexToPosition (voxelInfo.index),
           transform.rotation ) as GameObject;

In what way do you want to rotate it? do you want to rotate it relative to a parent gameobject, the current gameobject or do you want to rotate the object around it’s own axis?

does that syntax even work?

Instantiate(...,  transform.position = <something>, ...)

surely it should be

Instantiate(...,  <something>, ...)

at the very least if that assignment works within the instantiate you’re moving the calling gameobject and not the instantiated instance to that position

ok so im trying to Rotate it on its X Axis because for some reason when its instantiated its on its side so im trying to make it upright its just been along time since ive actually needed to rotate something in code so im not exactly 100%sure how to do it aha…

Hey thanks for the reply its not Instantiating the object thats the problem its simply rotating it on its X Axis i havent needed to rotate something in code for quiet some time now and i have litterally forgotten how to rotate an object in code … lol …

GameObject TorchFireParticles = Instantiate(Resources.Load("TorchFire"), transform.position = voxelInfo.chunk.VoxelIndexToPosition (voxelInfo.index),
Quaternion.Identity ) as GameObject;

Quaternion.Identity gives back 0 on all rotations, so it will instantiate with the default rotation as saved on the prefab.

Ohh i see although it still seems to instantiate on its side for some weird reason :l …

Then check your prefab. Most likely you have saved it with the wrong rotation. Or it is the weird position thing you do in the code that is causing issues.

The positioning is so that it will instantiate to the position of the voxel(cube) i place so im not entirley sure but i dont think the position would be causing the issue and as for the prefab ive already check before i made this thread the prefabs all set to 0 on all 3 Axis for the rotation

Well the way you call instantiate now makes no real sense, so it could cause the issue. If you want to position it at the position of the voxel ,then just do this:

var voxelPosition = voxelInfo.chunk.VoxelIndexToPosition (voxelInfo.index);
GameObject TorchFireParticles = Instantiate(Resources.Load("TorchFire"), voxelPosition,
Quaternion.Identity ) as GameObject;

if you used blender to create the model the prefab is using there is an issue with blender and unity using different axis (left hand up and right hand down or some such) which can mean the model appears “on it’s side” in unity.

Hey thanks for the Help i managed to fix it aparently unity when instantiating particles for some reason they emit the particles on their side (i have no idea why) but in the end the only way to fix it and make it actually work for what i was trying to achieve was to create a child object to place the particles in and the just rotate the child object to my likeing that seems to fix it hanks once again for the help :slight_smile: