- Home /
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:
post your scripts here - at the very least, the controller script...
Nobody is going to s$$anonymous$$l your script that doesn't work. Post the relevant bits here and we can get it sorted.
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..
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).
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.
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.