Is there a way to turn on/off a script? Same question for the dynamic engine.
Thanks,
Robert
Is there a way to turn on/off a script? Same question for the dynamic engine.
Thanks,
Robert
just deactivate it.
Don’t know what a dynamic engine is, as there is no such thing. if you talk about the physics, then no. Its the core of many things including collision and raycasts. Also I don’t see why one would want to disable it as it does not cost performance if not used
What I mean is can I turn on/off a script while the game is playing. For example, I have a script that controls and object with the accelerometer. At one point I want to turn off the script so it doesn’t affect the object anymore and turn it back on later.
As for the dynamics, let’s say I have an object affected by a collision. So it moves in space. I’d like to stop the dynamic simulation and reset the position of the object.
Yes as mentioned, just deactivate it by setting its enabled (not active) property to false.
If you only use colliders and nothing else that should be up to your code
If you want to disable it inside the script, you can just add a boolean that needs to be true for any of the physics stuff to calculate.
var onOff : boolean = true;
function FixedUpdate()
{
if (onOff) {
// Physics stuff
}
}
Cool, thanks!
About the script answer. I appreciate the answer but it doesn’t help me much. I need to know HOW I can turn a script on or off and you reply to just turn it active or false… What I need is the syntax.
Robert
Sorry my error, naturely meand the enabled property.
Active is for game objects
Actually it is recommended to use the property enabled.
Checking to see if the script is active in FixedUpdate is also discouraged as it still uses resources to enter the function.
Use something like this:
add this to your accelerometer script.
function OnDisableScript()
{
enabled = false;
}
function OnEnableScript()
{
enabled = true;
}
// YOUR CODE
You’ll have to call the enable/disable functions from the outside, as your script now won’t receive any events generated by Unity.
Something like this:
var yourAccScript;
function EventThatCausesDisable()
{
yourAccScript.OnDisableScript();
}
function EventThatCausesEnable()
{
yourAccScript.OnEnableScript();
}
Ah! Thanks! That’s what I was looking for!
True, but sometimes you need an option that allows you to use non-Physics code in the same script while the Physics code is disabled.
The resource use of the check every frame is minimal until your scripts get into the thousands, besides.
If you have those kind of dependencies then it is usually better to split the one script into two.
It’s just a matter of seperating your game-logic code from unity-engine-interacting code.
But it really doesn’t matter that much, as you have already mentioned.
Not on the iphone
Already hundreds are deadly
Deactivate in which manner.
Can you pls specify
To enable / disable a script
First you want to declare a variable that will tell which object the script to be turned on/off is attached to, in that case the camera:
var Cam : GameObject;
Then you need another one to tell which script you want to turn on/off
private var MyOnOffScript : myScript;
Then you “link” them together:
MyOnOffScript = Cam.GetComponent (myScript);
Finally you enable/disable the script
MyOnOffScript.enable = false;
or
MyOnOffScript.enable = true;
Hope that helps.
Robert
Thats much clarified riouxr.
Thanks
So have you been able to make it work?
I tried to give you an answer as clear as possible because there’s a lot of people on these forums that don’t have a lot of experience doing this (like me!) and it’s always nice to have a clear explanation. It’s much nice then somebody telling you to read the doc…
Or to ask a question like “how do I make a plane fly” and get an answer like “just follows the laws of physics”. What I want to hear is “Everything is in the wings. You have to shape them in a way that it will create a depression on the top of it that will suck it up, check this out for more info blah blah blah”. At least you now which direction you need to go.
Also I like to share the answer once I found it because that’s the goal of a community, to help each other. It’s better then: “Never mind, I found it” without giving away the solution.
Good luck!
Yaa, actually the way you told i got it straight thru the problem i was facing.
Thank you very much, Its the people like you that makes the place a better learning place. I have also got reply like “go thru the doc”. But thats ok nevermind its just some of those people rest of the community do help us out.
And as such there are guys like you helping the community and grwoing phase, thanks once again