- Home /
Start and Awake running twice?
I'm somewhat new to Unity and scripting in C#. I have a script attached to a gameobject, and set a value in the start function. However, the start function seems to be running twice. I've double checked that I only have 1 instance of the script attached to the object, and I've tried removing the script from the gameobject and re-adding it, to no avail. I have also tried running the code in the awake function, but that also runs twice.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class destroyOnHit : MonoBehaviour
{
public GameObject explosion;
// Start is called before the first frame update
public bool destroy;
private void Awake()
{
Debug.Log("awake");
}
void Start()
{
Debug.Log("Framecount: "+Time.frameCount);
Debug.Log("Start: "+destroy + ", "+gameObject.transform.position.ToString());
}
// Update is called once per frame
void Update()
{
}
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Bullet")
{
Debug.Log("hit");
Debug.Log(destroy + ", " + gameObject.transform.position.ToString());
Instantiate(explosion, new Vector3(transform.position.x, transform.position.y, transform.position.z), Quaternion.identity);
if (destroy)
{
Destroy(transform.parent.gameObject);
Debug.Log("destroyed");
}
else
{
transform.parent.gameObject.SetActive(false);
Debug.Log("set inactive");
}
}
}
}
So when you run you get 2 awake messages and 2 framerate and 2 start messages in your console?
Add a second argument to the Log function.
Debug.Log("Framecount: "+Time.frameCount, gameObject);
If you click on the message in the console, the gameObject emitting the message should be highlighted in the hierarchy.
If the same object is highlighted twice, you have attached the script to the same game object twice. Otherwise, you may have simply attached the script to 2 different gameObjects.
Both print statements are highlighting the same gameobject, but I've checked, and that gameobject only has the script attached once.
What if you remove the component from the gameObject, is the message visible again in the console?
Answer by Llama_w_2Ls · Jul 28, 2020 at 10:20 AM
bool Once = false;
private void Awake()
{
if (Once == false)
{
//Do Action
Once = true;
}
}
Adding this little snippet will only allow whatever is in the awake to run once. However, you do need to check if any other scripts or functions are calling the same start or awake methods more than once because this shouldnt be happening
Answer by Orion336 · Jan 07, 2021 at 10:40 PM
I ran into the same problem today but found my error. In the script which instatiated the gameobject I later wanted to also set the instance to a gameobject so I could set the parent but forgot to comment out the initial instantiation.
Somehow this created an unvisible duplicate. The whole script ran twice, Llama_w_2Ls code didn't work but there was only one gameobject visible underneath the parent and on transform.name both times the code ran, the name was gameObject(Clone).
I tried to set a debug for transform.parent.name and 1 returned a nullrefference.
No solution here but I hope it helps with debugging.
the name was gameObject(Clone).
Are you saying that you possibly instantiated the same object? This would create two instances of the same script and so my code snippet should not work. Having two instances, will run two Awakes. No surprise there...
That was the odd thing, it seemed like there were 2 instances but both of them had the same isntance name ( gameObject(Clone) ) and there was only 1 instance visible in the hierarchy as child of the parent.
Your answer
Follow this Question
Related Questions
Awake called after I activate object, not after Instantiate... is it normal?? 2 Answers
Function that triggers something as soon as Object is "Set Active" 3 Answers
awake and start functions not working 2 Answers
start called directly after instantiate? 1 Answer
If a script is attached to more than one gameObject, will Start() & Awake() run more than once? 2 Answers