- Home /
Not instantiating prefab in the right place after 2017 update
This is the code I'm using:
//Creates a Matrix with sizexsize
Transform canvas = GameObject.Find("Canvas").transform;
matrix = new GameObject[size, size];
float tileSize = 30.0f;
float corrector = tileSize * size / 2;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
float tempX = i * tileSize + Screen.width / 2 - corrector;
float tempY = j * tileSize + Screen.height / 2 - corrector;
matrix[i,j] = (GameObject)Instantiate(prefabTile,
new Vector3(tempX, tempY, 0),
Quaternion.identity, canvas);
//CHECK: For some reasons all the gameObjects are getting
//instantiated in the same spot ( the middle of the canvas )
print(matrix[i, j].transform.position);
matrix[i, j].name = string.Format("[{0},{1}]", i, j);
//This next line solves the problem
matrix[i, j].transform.position = new Vector3(tempX, tempY, 0);
}
}
I'm trying to spawn different button objects set in a panel form in canvas. Before the update ( from 5.6 to 2017.3) it worked as expected. After the update all the prefabs, which are mainly unity buttons, instantiated in the middle of the canvas. I checked that the position vector I was passing was correct and it is. To solve this problem I have to set the position again after the instantiation with the same exact values I passed on to the instantiate function.
I was able to find a momentary solution for this but I'd like to know why this might be happening to have a better understanding.
Thank you in advance!
Answer by Cornelis-de-Jager · Jan 23, 2018 at 10:55 PM
Your code looks fine, if it is breaking after an update I would suggest deleting the class and creating a new one.
I just tried but unfortunately it didn't work. It's completely ignoring the position I give it to instantiate in. If the canvas has 400 x 200 it will instantiate the prefab at 200 x 100 regardless of the position I pass in the instantiate function. Did this parameter become useless when in Canvas in the new Update?
Answer by YagoV · Jan 27, 2018 at 06:58 PM
Any Ideas???
I recently updated another project from 5.6 to 2017 and something similar happened. Before the update the bullet would have a force applied in the direction of the mouse cursor, but for some reason now it's firing up all the time. It seems like the rigidbody.AddRelativeForce is just acting like rigifbody.AddForce.
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit))
mousePosition = hit.point;
GameObject bulletGO =(GameObject)Instantiate(bulletPrefab,
transform.position,
Quaternion.identity);
bulletGO.transform.LookAt (mousePosition);
Rigidbody bulletRB = bulletGO.GetComponent<Rigidbody> ();
bulletRB.AddRelativeForce(Vector3.forward*1000);
Your answer
Follow this Question
Related Questions
How do i get the UI button position? 2 Answers
Get "CanvasSpace" position of UI element 1 Answer
instantiating elements in UI/Canvas 1 Answer
Can you Instantiate a Canvas into a scene? (Not objects inside of a canvas, but the canvas itself) 0 Answers
How to get accurate x/y for Rectangle Transform of Panel under canvas? 2 Answers