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 naf456 · Nov 06, 2011 at 09:41 AM · prefabsceneobjectsdetect

Best way to detect if a prefab is already in the scene?

Hello again :). I have got several scripts, like a music player script and a GameState Script, Which I would like to keep active throughout all scene's. I Have managed to do this with "don't destroy on load". My problem is when I get back to the main menu - they re-instantiate again with scene... I have tried checking for objects with the same tag - and deleting the game object if another is found already in place - yet it's not working... Somehow I feel like is stupidly easy... This is what I've got at the moment :

 GameObject thisDup = GameObject.FindGameObjectWithTag("System");
 if(thisDup)
 {
     Destroy(gameObject);
 }

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by chemicalvamp · Nov 06, 2011 at 11:05 AM

 Hmm try this out:

     void Awake() 
     {
         public GameObject[] duplicates = GameObject.FindGameObjectsWithTag("System");
         foreach (GameObject gameObject in duplicates)
         {
             Destroy(gameObject);
         }
     }
 

Might be some errors, threw it together quick in notepad

Comment
Add comment · Show 5 · 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 aldonaletto · Nov 06, 2011 at 12:12 PM 0
Share

You can't have a variable called gameObject because it's the name of a property. Change it's name to anything else:

    foreach (GameObject dup in duplicates)
    {
        Destroy(dup);
    }
You should also do it at Start, because Awake may occur in this particular object before the duplicate has been created.
avatar image gfr · Nov 06, 2011 at 12:19 PM 1
Share

You can have a local variable named gameObject, it will just shadow the property.

avatar image chemicalvamp · Nov 06, 2011 at 12:23 PM 0
Share

lol true true, I can blame notepad right? :D

avatar image syclamoth · Nov 06, 2011 at 12:27 PM 0
Share

Yeah, having a local variable named gameObject will technically work, but that doesn't stop it being disgusting.

avatar image Bunny83 · Nov 06, 2011 at 01:02 PM 0
Share

True words :D. It's just bad practise cause it can produce unexpected results. In some cases you might want to hide a property but it should be avoided since it makes the code very hard to read. If you work with other programmers you should write clear code.

Anyways, it's a solution ;)

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

7 People are following this question.

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

Related Questions

How can i transfer an object from scene1 and instantiate it in scene2? 2 Answers

different game "scenes" and optimization? 2 Answers

Empty Project & Scene, showing up tons of loaded stuff in Unity 4.3.3f1 and not in 4.3.1f1? 0 Answers

Passing a prefab between scenes? 1 Answer

How top create prefab with simple cube using YAML? 1 Answer


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