- Home /
Object cannnot be transformed in start()
I'm a newbie of using the unity. I wonder why how I can't transform the instantiated object in start(). However, if I transform the object in Update(), it works immediately. Could somebody tell me what is the reason behind? Thanks T.T
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class Gameplay : MonoBehaviour {
public GameObject pausePanel;
public GameObject loadingPanel;
public GameObject[] monsters;
private int row = 6;
private int col = 1;
private static float horizontalSpacingOffset = -3.0f;
private static float verticalSpacingOffset = 3.0f;
private static float borderSpacing = 0.0f;
List<GameObject> monstersList;
bool start;
// Use this for initialization
void Start () {
monstersList = new List<GameObject>();
pausePanel.SetActive(false);
for (int y = 0; y < col; y++)
{
for (int x = 0; x < row; x++)
{
Vector3 location = GridToWorldPoint(y, x);
GameObject monster = (GameObject)Instantiate (monsters [5], location , transform.rotation);
monstersList.Add(monster);
}
monstersList[3].transform.position = new Vector3(0, 1, 0);
}
start = true;
}
// Update is called once per frame
void Update () {
}
#region GetInitialLocation
//finding the grid location
private static Vector3 GridToWorldPoint(int x, int y)
{
return new Vector3(x + horizontalSpacingOffset + borderSpacing * x,
-y + verticalSpacingOffset - borderSpacing * y, 0);
}
#endregion
Answer by christuart · Aug 21, 2015 at 11:36 AM
Often if you have some code which appears to function when in an Update but not when run straight after instatiation, it is because your change is being unmade by some other code before you can see it.
Check that your 'monsters[5]' GameObject doesn't have a script attached with a Start() method altering its transform.position. When you instatiate an object, all Awake() methods in attached scripts are run immediately, but the Start() methods don't get called until just before the first Update(), so after the Gameplay.Start() code you have posted would have already run.
If you can't identify any code in a Start() method that might be changing the transform.position from the Vector3(0, 1, 0) you have specified in Gameplay.Start(), you could use
Debug.Log(<a reference to the transform.position you want to have changed>);
in various locations, including Awake() and Start() methods in the 'monsters[5]' GameObject to try to find when it is changing. Remember that the running order will be:
Any Awake() methods in the monster GameObject
Your attempt to change the transform of one of those monsters
Any Start() methods in the monster GameObject
Any Update() methods in the Gameplay GameObject and the monster GameObject, in an unknown order.
Are you absolutely certain that the code doesn't work as you've posted it? Depending on your scale, it may be difficult to see the effects directly in the scene or game views. Are you certain you're checking the correct instance of monsters[5]? Are you aware that you are only changing the position of one of the 6 instances? (Just checking!) And finally, have you realised that if you included more than one column then your code to try moving that one instance would be called multiple times, as it is inside the columns for loop?
Your answer
Follow this Question
Related Questions
How to Return Enemy to startPosition 1 Answer
Return Enemy To Start Position 1 Answer
Remember the start coordinates? 2 Answers
can't use camera position in equasion, help! 2 Answers
transform position vectorially 2 Answers