Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Luponius · Jun 06, 2016 at 01:39 PM · scripting problemrestart

Rerunning a script on a game object

So I have a game object called MapController which has a script called MapManager attached to it.

Map Manager basically creates an entire map and runs analysis etc on it. Now I need to this multiple times over, and unfortunately my code is a total spaghetti mess, and I'm short on time due to this being a thesis deadline. How can I have the same process run multiple times without rewriting all of my code?

I am hoping to accomplish something along these lines:

 GameObject.Find ("MapController").GetComponent<MapManager> = new MapManager();

Which would hopefully rerun the Start method, even though it's a really bad way of going on about it. Could something like this work somehow? Any other alternatives which don't require me to recode everything up (I don't have enough time to do so until submission since this is already multiple months of work, and multiple changes already committed to this project).

Comment
Add comment · Show 3
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 Hellium · Jun 06, 2016 at 01:44 PM 0
Share

If code is supposed to run multiple time, never put it in a Start function, but in its own (public) function. Then, you will be able to call this public function from whenever it is needed.

avatar image NoseKills · Jun 06, 2016 at 04:26 PM -1
Share

How about making a method that Destroy()s the script and then uses AddComponent again? Or one that Destroy()s the whole GameObject and creates a new one and adds a new script to it?

Any cleaning up you need to do could be done in OnDestroy or OnDisable in the controller

avatar image Hellium NoseKills · Jun 06, 2016 at 06:25 PM 0
Share

Destroying and instantiating components / gameobjects is resources consu$$anonymous$$g. Here, a simple call to his (heavy) "analysis" function will be far better.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by angelx3 · Aug 09, 2016 at 08:02 AM

 var mapController = GameObject.Find ("MapController");    
 if(mapController != null){    
   var mapManager = mapController.GetComponent<MapManager>();    
   if(mapManager ==null){    
     mapManager = mapController.AddComponent<MapManager>();    
   }    
 }
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

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

6 People are following this question.

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

Related Questions

Best way to Start/Stop script during runtime. 0 Answers

Can't restart a level 2 Answers

Editing generic list through editorwindow 0 Answers

My C# Script not working 2 Answers

[SOLVED]Why does my Timer not work? (Called by button Script) 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