Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by UnityMai · Dec 12, 2016 at 09:21 PM · scripting problemcomponentdisablesmall

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!

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 UnityMai · Dec 13, 2016 at 07:48 AM 0
Share

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.

avatar image UnityMai · Dec 13, 2016 at 10:13 AM 0
Share

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;
 

avatar image UnityMai · Dec 13, 2016 at 01:11 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 6 · 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 UnityMai · Dec 13, 2016 at 10:30 AM 0
Share

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.

avatar image UnityMai · Dec 13, 2016 at 10:34 AM 0
Share

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?

avatar image UnityMai · Dec 13, 2016 at 10:41 AM 0
Share

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.

avatar image LoBlanc UnityMai · Dec 13, 2016 at 10:56 AM 0
Share

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?

avatar image UnityMai LoBlanc · Dec 13, 2016 at 01:01 PM 0
Share

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".

avatar image UnityMai · Dec 13, 2016 at 12:47 PM 0
Share

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.

avatar image
0

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

Comment
Add comment · Show 2 · 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 LoBlanc · Dec 13, 2016 at 10:17 PM 0
Share

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

avatar image UnityMai LoBlanc · Dec 14, 2016 at 06:26 AM 0
Share

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 :).

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

99 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

HELP I would like a button that leverage the AAA graphics 0 Answers

Storing any component inside a single variable. 1 Answer

I'm receiving an error saying that "Specified cast is not valid (wrapper castclass)". 0 Answers

script does not disable 0 Answers

How to write type-safe loosely coupled components 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