- Home /
how can I disable and enable a privte void in the same script with a line of a cod
if Im doinig GameObject.Find("swat").GetComponent().enabled = false; so in the next I cant enable it like that GameObject.Find("swat").GetComponent().enabled = true; so what can I do to disable only a void like performMovment() or any other void. please help. (sorry for bad English)
are you Invoking it? if so CancelInvoke("$$anonymous$$ethodName"); works for that.
Answer by xxmariofer · Feb 21, 2019 at 02:43 PM
hello, you cant disable a "void". void is returned type (which means they dont return a type) the enabled property stops the update calls, if you dont want to call one specific method just dont call it, and if you calling it in the update create a bool for controlling when you call it.
Not sure that's Quite what they meant but good*coment* ;)
you dont disable methods you just dont call them. if you dont call it it will not get invoked, can you share your code so i can try to give you an example?
using System.Collections; using System.Collections.Generic; using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class player$$anonymous$$oto : $$anonymous$$onoBehaviour {
private Vector3 velocity = Vector3.zero;
private Vector3 rotation = Vector3.zero;
private Vector3 cameraRotation = Vector3.zero;
public Animator Anim;
public bool run;
[SerializeField]
private Camera cam;
private Rigidbody rb;
private void Start()
{
rb = GetComponent<Rigidbody>();
// Store a copy of your cullingmask
_mymask = cam.culling$$anonymous$$ask;
// Only render objects in the first layer (Default layer)
cam.culling$$anonymous$$ask = 1 << 0;
// do something
}
public void move (Vector3 _velocity )
{
velocity = _velocity;
}
public void rotate(Vector3 _rotation)
{
rotation = _rotation;
}
public void rotateCamera(Vector3 _cameraRotation)
{
cameraRotation = _cameraRotation;
}
private CharacterController controller;
private float gravity = 14.0f;
private float verticalVelocity;
[SerializeField]
private float jumpForce = 10.0f;
private void FixedUpdate()
{
perform$$anonymous$$ovment();
prformRotation();
//animations
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W)) //forward 1
{
Anim.SetInteger("walk", 1);
}
else if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.W)) //forward 0
{
Anim.SetInteger("walk", 0);
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.S)) //backward 1
{
Anim.SetInteger("walk backword", 1);
}
else if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.S)) //backward 0
{
Anim.SetInteger("walk backword", 0);
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.V))
{
Anim.SetBool("dance?", true);
GameObject.Find("swat").GetComponent<player$$anonymous$$oto>().enabled = false;
}
else if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.V))
{
Anim.SetBool("dance?", false);
GameObject.Find("swat").GetComponent<player$$anonymous$$oto>().enabled = true;
}
}
void perform$$anonymous$$ovment()
{
if (velocity != Vector3.zero)
{
rb.$$anonymous$$ovePosition(rb.position + velocity * Time.fixedDeltaTime );
}
}
void prformRotation()
{
rb.$$anonymous$$oveRotation(rb.rotation * Quaternion.Euler(rotation));
if (cam != null)
{
cam.transform.Rotate(-cameraRotation);
}
}
int _mymask;
}
the easiest solution is to put the perform$$anonymous$$ovment(); line inside the updtae in an if statement like this
if(wantToPerform$$anonymous$$ovement){
perform$$anonymous$$ovment();
}
and when you dont want to perform$$anonymous$$ovment you can just do this
wantToPerform$$anonymous$$ovement = false;
and the method wont be called and set it again to true for calling it again
ty very much I did x = 4; if(x == 4){ perform$$anonymous$$ovment(); }
x = 2 // stops the moves
Answer by OscL1 · Aug 06, 2020 at 10:44 PM
You don't disable a method, you just don't call it, say I have a method --> Void A(){Debug.Log("Hi");} --> and I call it in update --> void Update(){A();} --> but I want the console to stop printing out Hi 30 seconds after the games, I can simply add a if statment --> if(Time.time < 30){A();} --> There is also a lot of ways to do this, an if statment is the simplest like you can use a Coroutine or **InvokeRepeatin**g to repeat a method not every frame but a set amout of second that you want it to be, or you can use the switch statment to check on a value, and more and more...
Your answer
Follow this Question
Related Questions
Enable/Disable Game Object With GUI Button 1 Answer
Enabling loop and disabling loop 1 Answer
Enable/Disable a Static object 1 Answer
How to change settings to a rigidbody when adding it to a gamobject? 2 Answers
Gui.Enable transperancy 4 Answers