Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by NS428 · Jul 26, 2020 at 12:33 AM · startawake

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");
 
             }
         }
     }
 }
 
Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image logicandchaos · Jul 26, 2020 at 02:30 AM 0
Share

So when you run you get 2 awake messages and 2 framerate and 2 start messages in your console?

avatar image NS428 logicandchaos · Jul 26, 2020 at 06:50 PM 0
Share

Yes, that is correct.

avatar image Hellium · Jul 26, 2020 at 07:04 PM 1
Share

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.

avatar image NS428 Hellium · Jul 27, 2020 at 07:57 PM 0
Share

Both print statements are highlighting the same gameobject, but I've checked, and that gameobject only has the script attached once.

avatar image Hellium NS428 · Jul 27, 2020 at 08:07 PM 0
Share

What if you remove the component from the gameObject, is the message visible again in the console?

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Llama_w_2Ls · Jan 08, 2021 at 10:43 AM 0
Share

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...

avatar image Orion336 Llama_w_2Ls · Jan 08, 2021 at 10:46 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

137 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges