C# (Changing Material, Instantiation, Positioning)

Hello,

Just started using C# due to it’s powerful regular expressions. I am running into problems that were quite easily solved in Unity JS.

I instantiate a prefab:

GameObject newMino = Instantiate(Resources.Load(“Prefabs/Mino”)) as GameObject;

Is there a way to instantiate at a certain location? If not, can you specify a position rather than translating it after instantiation? I.E.,

Transform t_newMino = newMino.GetComponent(typeof(Transform)) as Transform;
t_newMino.position = Vector3(5, 0, 0);

Lastly, the prefab has 4 materials. By default, the prefab uses the last material in the array. I want to change the active material to be, say, the first material in the array. How can I do this in C#? I have tried the following with no luck:

Renderer r_newMino = newMino.GetComponent(typeof(Renderer)) as Renderer;
r_newMino.renderer.material = r_newMino.renderer.materials[0];

and

Renderer r_newMino = newMino.GetComponent(typeof(Renderer)) as Renderer;
r_newMino.renderer.material = r_newMino.renderer.sharedMaterials[0];

Thanks!
-n

Having issues with the same thing, and I see this wasn’t ever answered.

I was using transform.position.x = 10, or something similar to that, however this simply does not work in C#. Is there anything equally simple that I am missing?

Well I figured out I could just create a new Vector3, set the position I want there and then set transform.position = myVector3, but is there any other way to do this directly perhaps? Seems a bit convoluted…why would the x position of a position be read only in C# but be writable in JS?

Transform is a property, as such you can’t alter its fields content.
What you need to do is store the position in a vector, alter it and store it back