It’s very simple to change the position and rotation. I’ve modified your code a bit so both position and rotation is modified. As you can see you can access the transform of the gameobject where the script is placed directly. Good Luck!
void OnGUI(){
if (GUI.Button (new Rect (Screen.width * guiPlacementX1, Screen.height * guiPlacementY1, Screen.width * .2f, Screen.height * .3f), "", Display)) {
if (int.Parse (Square) == 1) {
transform.position = new Vector3(7.0f, 7.0f, 7.0f);
transform.rotation = Quaternion.Euler(new Vector3(45, 45, 45));
}
}
}
Thank you for your modified code but when I placed your code, the gameobject doesn’t print anymore and when I tried to remove the transform.rotation = Quaternion.Euler(new Vector3(45, 45, 45)); the gameobject already displayed. But the position is still the same and the gameobject became bigger. Why?
First off, don’t name your variable the same as your class. Your class is named asd, and you have a Transform variable named asd.
I’m going to guess that it’s the instantiated object that you want to position? (it would be helpful if you explain what this script is trying to do). If it’s the instantiated object you want to move, it would be like this (the part inside if(int.Parse(Square) == 1)):
Transform newKahon = Instantiate(kahon) as Transform;
newKahon.position = new Vector3(0f,0f,100f);
// you can also do like what Jenzo83 mentioned for rotation:
newKahon.rotation = Quaternion.Euler(new Vector3(45, 45, 45));
My script is trying to display an imported fbx file when a button is clicked. So, the gameobject will not display until the button is clicked. Square is where the number will be inputted. The number that will be imported is for: when the inputted no. is 1 and the button is clicked, the gameobject will print. Now, I’m trying to change the position of the gameobject through code. I’ve tried this code:
transform.position =new Vector3(5,5,5);
print(transform.position.x);
but it did not work on my class. So I tried to try that code in another new project to try if it’s working. So I tried it in a cube. It’s a gameobject from the unity. On that sample class, I did not put a button and int parse. I just put it in the start function and dragged the script in the cube on the hierarchy. And it works! But on my own class named as asd. It really didn’t work. Did you get my explanation? I’m sorry if I’m not that good in English.
You’re pretty close to right. The issue with your original code is that you are setting the position of the “kohan” object instead of your newly instantiated “asd”. Your code should probably look something like this:
void OnGUI()
{
if (GUI.Button (new Rect (Screen.width * guiPlacementX1, Screen.height * guiPlacementY1, Screen.width * .2f, Screen.height * .3f), "", Display))
{
if (int.Parse (Square) == 1)
{
Transform asd = Instantiate (kahon) as Transform;
asd.transform.position = new Vector3(0f,0f,100f);
asd.transform.rotation = Quaternion.Euler(new Vector3(45f,45f,45f));
}
}
}
So, I take it you want to make a model viewer? Or even something like SSB’s trophy section?
In that case, do the instantiation in a new variable, then manipulate it. perhaps have it so that it only instantiates when the variable for switching changes, to save on performance.
If you wish me to post a simple example of this, give me another PM, or post here!
Yeah I got it (ok lang magtanong ng Tagalog dun sa Unity Philippines Users Group). I suggest you get a good tutorial on the fundamentals of Unity. The official learn section has a bunch of them. I also made some tutorials before that seems to be appreciated by a lot of people.
Thank you for your answer but the position is still not changing. Also the rotation. I figured it out because I’ve tried to change the number of position, but still the position still stays the same.