- Home /
Start() not called upon instantation?
Hi guys,
I have a script attached to a poop object:
...
function Awake() {
Debug.Log("Poop created");
}
function Start () {
Debug.Log("Poop at x:" + transform.position.x + " y:" + transform.position.y + " z:" + transform.position.z);
...
}
function Update () {
Debug.Log("Poop update");
...
}
...
But when the object that has the above script attached is instantiated somewhere else...
function Poop() { poopChute.Play(); ... //Instatiate a poop pellet and drop it with an initial velocity of 1m/s opoosite to the magnitude of the pigeon var newPoop = Instantiate(poop, poopChute.transform.position, Quaternion.identity ); var poopScript : Poop = newPoop.GetComponent("Poop"); poopScript.velocity = rigidbody.velocity; }
...
The first script's Start() and Update() functions aren't being called at all, even though Awake() works ok. I can't test whether my poop's homing mechanism works until this problem gets fixed. What's going on?
MachCUBED
i just copy pasted ur 1st part and it works for me. u should copy all ur script o whatever u have
Answer by Kryptos · Aug 22, 2012 at 06:20 PM
When is function Poop()
called? I can't see it in your script.
Anyway, I see two possible mistakes here:
The
Poop()
method has the same name than the script. But it is forbidden with Unity to redefine constructor of Scripts. I don't know if this function is considered as a constructor in JS/US but in C# it will and thus not work.Make sure that the
Poop()
method is called, that the poop prefab does have aPoop
script as component and that this script is not disabled by default (same with the GameObject), because disabled scripts do callAwake
but notStart
neitherUpdate
.
edit: other possibilities:
Do you set Time.timeScale to zero somewhere?
How are you sure that your function are not called? Maybe you forget to uncheck the "collapse" button on the console.
I changed the name of my script to PoopBehavior from Poop, and the script attached to Poop (the GameObject) is enabled by default. it still isn't calling Start() and Update(). Bummer.
I just guess you have disabled the component on the prefab? So the clone has the script also disabled.
I've already said that the component is enabled by default on the prefab. And Time.timeScale is only set to 0 when the game is paused. It doesn't happen at all on PoopBehavior.
I tried adding this:
Time.timeScale = 1;
to Awake(). It made no difference, but I deter$$anonymous$$ed that the problem was being caused by OnCollisionEnter():
function OnCollisionEnter(collision : Collision) {
// Not needed in the game environment after it hits something
Destroy(this.gameObject);
}
The poop appears to be hitting the inside of a collider upon instantiation. So it's a positioning problem.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Update and Awake not being called. 1 Answer
Start, Awake, Update. Any other ways to call functions from an empty GameObject? 3 Answers
FindGameObjectWithTag wont find in Start() or Awake() but will in Update() 1 Answer
C# void names 1 Answer