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 ravikumarjogu · Jun 30, 2014 at 09:13 AM · c#programmingenableenabled

Re enable the script to recover the initial variables

My Flow:

(go1.GetComponent (typeof(phase1)) as phase1).enabled = true;

(go1.GetComponent (typeof(phase1)) as phase1).enabled = false;

(go1.GetComponent (typeof(phase1)) as phase1).enabled = true;

I could not able to recover the transy intial value on re-enabling

phase1.cs

using UnityEngine; using System.Collections;

public class phase1 : MonoBehaviour {

 // Use this for initialization
 public Texture2D struc,activit;
 public string next1, next2;
 private float transy=(int)(Screen.height*1.2f),transend=(int)((float)Screen.height/1.4);
 void Start () {
     transy = (int)(Screen.height * 1.2f);

 }
 
 // Update is called once per frame
 

 void OnGUI(){
     Debug.Log ("I am active");
     //GUI.color = Color.clear;    
     if (transy > transend) {
         transy-=4;        
     }
 }

}

What I want is: void Reinitialize(){ //initalize all variables again } Is there any method like this which provides for re-initializing variables

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

2 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by gjf · Jun 30, 2014 at 09:18 AM

from the docs:

http://docs.unity3d.com/ScriptReference/MonoBehaviour.Start.html

"Start is called on the frame when a script is enabled just before any of the Update methods is called the first time."

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 Kiwasi · Jun 30, 2014 at 09:16 AM

There is no reason you can't wrap your initialisation method and call it as often as you like.

 void Start (){
     myCustomStart();
 }
 
 void myCustomStart(){
     // Run all your initialisation here
 }
 
 void OnEnable (){
     myCustomStart();
 }
 
 // Inside any other method that makes sense
     myCustomStart();
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 Huacanacha · Jun 30, 2014 at 09:20 AM 0
Share

Is there any restriction on just calling $$anonymous$$onoBehaviour.Start() yourself?

avatar image Kiwasi · Jun 30, 2014 at 10:39 AM 0
Share

Technically no, but its a bad habit to get into.

avatar image HarshadK · Jun 30, 2014 at 10:52 AM 0
Share

And here's something more about using Start() in the answer to this question: How can I return back to script's "function Start()"?

avatar image Kiwasi · Jun 30, 2014 at 11:04 AM 1
Share

I'm not saying that calling Start() again is not possible. Its just bad coding practice. At some stage later you, or another developer, will come back to the code and add something else to Start() without realising its being called from all over the place. You are setting yourself up for hours of painful debugging. Its really not worth it for the cost of writing out one method.

avatar image Huacanacha · Jun 30, 2014 at 11:26 PM 0
Share

Yeah I agree it's bad practice given that Start() is a special Unity function (which should therefore be left to Unity to call), I was just curious whether Unity allows this. A dedicated initialisation function called from Start is certainly a better approach.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Storing my choose your own adventure questions/actions 1 Answer

Transforms are SUPER HARD! 2 Answers

How to find Gameobject with given coordinates 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