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 26, 2014 at 06:11 PM · c#

How do I stop a script from running from other scripts or from itself

I have four scripts(stage1.cs, stage2.cs, stage3.cs, stage4.cs) attached to an object in a single screen and I need to allow only one script will run at a time in a sequential manner.

I used a another public script(controller.cs) where I took a variable as 'stage' which is a integer it has two methods Increment() and Decrement(). My four scripts access the this stage based on value it will be executed. But I found only 1 and 4 are executing while 2 and 3 or not.

I could send my files if you want further clarification. plese attach mail id:

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 gjf · Jun 27, 2014 at 10:23 AM 1
Share

post your scripts here - at the very least, the controller script...

avatar image Kiwasi · Jun 27, 2014 at 11:06 AM 1
Share

Nobody is going to s$$anonymous$$l your script that doesn't work. Post the relevant bits here and we can get it sorted.

avatar image ravikumarjogu · Jun 27, 2014 at 12:26 PM 0
Share

using UnityEngine; using System.Collections;

public class controller : $$anonymous$$onoBehaviour {

      public int stage=0;
 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
 
 }
 public void setInc(){
     if (stage == 3) {
             stage = 3;        
         } else {
         stage++;        
     }
 }
 public void setDec(){
     if (stage == 0) {
         stage = 0;        
     } else {
         stage--;        
     }
 }

}

Common code for all scripts: except if(stage==stageNo)

  controller cl;

 cl=getComponentsInChildren<controller>();

  int stage=cl.stage;

  update(){

 if(stage==0){

 }

} Actions: controller cl; cl=getComponentsInChildren(); int stage=cl.stage; Update(){ if(stage==ActionNo) Recognize gesture input and perform some actions }

Problem is Actions were supposed to work at their stages but its not happening for 2 &3. The Action is working alone(I tested on the android device)

at the same time But I could observe only 1 and 4 are working..

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Ngoc Ngo · Jun 27, 2014 at 10:43 AM

Alternatively, you can keep only one "stage" script active at a time while disable the others. At the end of stage1 timeline, you active stage2 and deactivate itself(the same goes for stage3 and stage4).

Comment
Add comment · Show 1 · 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 Bunny83 · Jun 27, 2014 at 10:50 AM 0
Share

I'm pretty sure that's what his "controller" script is supposed to do. At least it sounds like that's what it should do.

avatar image
0

Answer by Bunny83 · Jun 27, 2014 at 01:57 PM

Where do you execute this line:

 int stage = cl.stage;

Is this line inside of Update? if not it will get the value only once and it will never change since it's an int. This line copies the value from your controller script into the local variable stage. If you don't update the local variable it will keep it's value forever.

Your pseudo code of your stage script isn't really helpful. also your approach isn't very efficient. You have all stage scripts running all the time an each one is checking the the same stage variable.

It's way better to have a Controller script like this (Note: class names should start with a capital letter):

 using UnityEngine;
 using System.Collections;
 
 public class Controller : MonoBehaviour
 {
     public MonoBehaviour stages;
     private int m_Stage = 0;
     public int Stage
     {
         get { return m_Stage; }
         set
         {
             m_Stage = Mathf.Clamp(value, 0,stages.Length);
             ChangeStage();
         }
     }
     
     void Start()
     {
         ChangeStage();
     }
     
     private void ChangeStage()
     {
         for (int i = 0; i < stages.Length; i++)
         {
             stages[i].enabled = (i == Stage);
         }
     }
     
     public void setInc()
     {
         Stage++;
     }
     public void setDec()
     {
         Stage--;
     }
 }

with a script like this you can simply assign all your stage scripts in the desired order to the "stages" array in the inspector. Whenever you change the "Stage" property, the controller will disable all scripts except the script with index "Stage". That way it's only 1 of the scripts enabled at the same time.

Inside your stage scripts you don't need to do anything. Update is only called when the script is enabled, so you would do whatever the stage should do directly inside Update.

Comment
Add comment · Show 1 · 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 ravikumarjogu · Jun 28, 2014 at 05:18 PM 0
Share

thanks Bunny this is a great help for me.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Is there any reason why I can't use 2 c# scripts on my Player? 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

Why a public varibale is not accsible inside Update() ? 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