Disable part of script
How to disable only a part of the script and not entire script? For instance if I wanted to disable a public void Spawner() how would I do that?
I know how to access it, but dont know how to disable "ONLY" Spawner() function within another script. I also know how to disable an entire sript, but no luck in trying to disable only a small part of another script.
So what i need to disable is only "gm2.Spawner()":
public class Player : MonoBehaviour
{
private GameManager2D gm2;
private bool isSpawned;
void Start ()
{
gm2 = FindObjectOfType<GameManager2D>();
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.CompareTag ("Horse")) {
if (!isSpawned) {
isSpawned = true;
gm2.enabled = true;
` gm2.Spawner();`
}
}
void OnTriggerExit2D(Collider2D col)
{
isGrounded = false;
{
if (col.CompareTag ("Horse")) {
if (isSpawned) {
isSpawned = true;
Destroy (hor);
/*
//This is what i wish to get DISABLED
//gm2.Spawner ();
//gm2.GetComponent<Spawner>().enabled = false;
//THIS ISNT WORKING, but this is what i would like to have
*/
Debug.Log ("Destroy HOR PLZ");
}
}
}
Please if anyone can help me with this problem, because I been struggling with this problem for months now. Thank you in advance!
No sorry that is not what i want. I quoted a part which i want disabled.
I need this:
gm2.Spawner()
to get DISABLED. The other part which i put:
gm2.GetComponent<Spawner>().enabled = false;
i just put as EXA$$anonymous$$PLE what i wish to achieve. This is what i need to disable only a public void Spawner() from an other script. I cannot disable an entire script (Game$$anonymous$$anager2D), because its built in a way that i need it to run, but i can disable a public void Spawner() and not destroy the code. I also tried breaking code into 2 scripts and still that didnt worked.
I use search i do research myself. I ask a simple question and I wish for someone to help me out. How else are we as individuals gonna grow and get better if we sometimes just dont get answers to our questions.
See for yourself what all i tried with no luck:
Instantiate(camO).GetComponent<Game$$anonymous$$anager2D> ().enabled =false;
//Instantiate(objectB).GetComponent<Game$$anonymous$$anager2D> ().Spawner ().enabled = false;
//gm2.Spawner;
//Instantiate(yourGameObject).GetComponent().enabled = false;
//spaw = objectB.Spawner ();
//spawn = FindObjectOfType<Spawn1> ( );
//Spawner.GetComponent<Spawner>().enabled = false;
//spawScript.Spawner() = false;
One more thing I can add.
What exactly I wish to do. I have a platform system which i need it to run only once after hitting a trigger zone "horse". After it leaves the trigger zone I wish to disable this platform system Spawner() which is part of another script called Game$$anonymous$$anager2D.
So thats why I called it in Player script gm2.Spawner(). Disabling a script is simple you just do gm2.enabled = false, but disable a part of this script is another story.
I am making a 2D platform jumper. $$anonymous$$y platform system will be different after each zone. And in order to do that I need to disable a part of that script and call a new function in this case would be gm2.Spawner2(), after that gm2.Spawner3() and so forth.
All help is appreciated, but a beginner cannot do it on his own sometimes.
Answer by LoBlanc · Dec 13, 2016 at 10:23 AM
If I understand, you just need to have an IF statement, with a boolean in its condition that indicates if what's next is being executed or not. It is so basic that I think I haven't undestood the problem. Hope it helps
Yes it is basic, but even basics do not help me if code does not work the way I want it to. And i have if statement all ready to go just the code does not want to get disabled and keeps running no matter what i tried. Yes i am still beginner after almost a year into developing with Unity, but I am trying really hard to understand as much as I can.
Some people claimed it is not possible to disable only a part of the code. This is what I am asking here. Is it even possible to disable only a "PART" of a code like a public void "Something"() or isn`t it?
If you look up i have "IF" statement all set up. What I do not have is a code which is executed correctly so that gm2.Spawner() gets disabled.
I want you to understand what you are coding. There is something strange. You wrote:
if (isSpawned) {
isSpawned = true;
...
}
What is inside the brackets is being executed when isSpawned evaluates true, so why setting again its value to true?
I wrote some of the code myself so that is why it is abit confusing and I apologise for that. I tried to make it as clean as possible.
isSpawned is a bool so it prevents the platforms to spawn more then once after hitting the trigger zone "horse".
I apologise i must have pasted it wrong its:
void OnTriggerExit2D(Collider2D col)
{
isGrounded = false;
{
if (col.CompareTag ("Horse")) {
if (!isSpawned) {
isSpawned = true;
Destroy (hor);
//gm2.Spawner ();
Debug.Log ("Destroy HOR PLZ");
}
}
But that still is not helping me disable a part of code what i wish. Thank you for trying to help, but so far none response has been helpful enough. I do not know how I will fix this problem on my own.
Answer by UnityMai · Dec 13, 2016 at 09:46 PM
This is what i found about this problem. I need someone to look at this and tell me if this is correct. That it is not possible to disable a part of a script or in other words a function.
http://answers.unity3d.com/questions/268505/disable-a-function.html
http://answers.unity3d.com/questions/217594/turn-off-function.html
I was trying to tell you that you only need to understand the "if" statement to select what you want to execute. Sorry if I didn't explain correctly. I hope you manage to solve it
Thank you I already know about abit how the if statements work. But this issue which I am facing according to this answer i pasted is not possible.
He said:
Answer by Bunny83 · Jun 17, 2012 at 01:55 A$$anonymous$$
That makes not much sense. A function can't be disabled. A function is something like a hammer. It can be used (functions can be called), but you can't disable functions. If you mean you want to prevent Unity from calling a certain callback function like Update, Start, OnCollisionEnter, OnEnable, ... then the answer is: in some cases ;) If you disable the script, Unity doesn't call Update and collision handling functions, but some are still called like OnBecameInvisible, OnBecameVisible.
So to sum up: You can't disable a single callback, but you can disable the whole script.
If this is the case then i have to rework my script.
Thank you for trying to help still :).