Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by caleb_b · Jun 23, 2014 at 09:02 PM · c#disableboolenable

Script won't reactivate after I deactivate it through a script

Hey all. I've got this script that is supposed to deactivate motion of the stock First Person Controller, and set a bool variable in a different script to true. Here it is:

 using UnityEngine;
 using System.Collections;
 
 public class LadderAcend : MonoBehaviour 
 {
     public GameObject player;
     private Climb climb;
 
     void Awake () 
     {
         player = GameObject.FindGameObjectWithTag("Player");
         climb = player.GetComponent<Climb>();
     }
 
     void OnTriggerEnter (Collider other) 
     {
         if(other.tag == "Player")
         {
             player.GetComponent<FPSInputController>().enabled = false;
             player.GetComponent<CharacterMotor>().enabled = false;
             climb.canControl = true;
         }
     }
 
     void OnTriggerExit (Collider other) 
     {
         if(other.tag =="Player") 
         {
             player.GetComponent<FPSInputController>().enabled = true;
             player.GetComponent<CharacterMotor>().enabled = true;
             climb.canControl = false;
         }
     }
 }

I have a custom movement script that is activated by the canControl variable being set to true.

When I enter the trigger, the Character Motor and FPSInputController are disabled, and canControl is true, but when I exit the collider, the scripts are not re-enabled, neither is canControl set to false. Any ideas?

Comment
Add comment · Show 14
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 AndyMartin458 · Jun 23, 2014 at 09:09 PM 0
Share

@caleb_b , are you sure that your player is in fact exiting the trigger? Set a debug statement in OnTriggerEnter and OnTriggerExit to make sure. Are you perhaps disabling the collider when you disable those components?

avatar image caleb_b · Jun 23, 2014 at 09:58 PM 0
Share

I put Debug.Log("Entered"); in OnTriggerEnter and Debug.Log("Exited"); in OnTriggerExit. "Entered" pops up, but only once. "Exited" never appears, even though I am far outside of the boundary of the collider, as shown here (the green box in the scene view is the collider, and the capsule is the player) alt text

I don't think the collider is changing at all, because I watch the colliders setting while in Play mode, and it is neither deactivated, nor set to "not trigger"

shaft.png (204.3 kB)
avatar image RyanPaterson · Jun 23, 2014 at 10:07 PM 0
Share

What happens if you use this?

 void OnCollisionExit(Collision other)
     {
         if(other.collider.tag =="Player") 
         {
 
             print("Somethings going on");
             player.GetComponent<FPSInputController>().enabled = true;
             player.GetComponent<Character$$anonymous$$otor>().enabled = true;
             climb.canControl = false;
         }
     }

avatar image caleb_b · Jun 23, 2014 at 10:14 PM 0
Share

Replacing my OnTriggerExit code with yours changes nothing. Changing my OnTriggerEnter to OnColliderEnter and making appropriate changes(True to false, visa versa), nothing happens when I enter the trigger.

avatar image Bunny83 · Jun 23, 2014 at 10:19 PM 0
Share

Well, the problem is most likely your player object. Do you actually have at least a kinematic rigidbody attached? Without one you don't get collision or trigger messages. Next thing is how does your "custom movement" script move your player? You might somehow bypass the physics system. You should add some more details about your playerobject.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ABerlemont · Jun 24, 2014 at 09:07 AM

After a script is deactivated through .enabled it cannot be found by a GetComponent. You need to put that reference in a variable.

GetComponent is process heavy, you should always call it at the start of the game and keep reference of what you need to interact with in variables.

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 caleb_b · Jun 24, 2014 at 01:06 PM 0
Share

Handy bit of info, and that seems like a more efficient way to handle references like those, but that doesn't do anything. OnTriggerExit is still not being called, unless I take out the deactivation codes.

avatar image ABerlemont · Jun 27, 2014 at 12:46 PM 0
Share

So your problem is that OnTriggerExit is never called ? That's not clear in your post. Is LadderAcent disabled elsewhere ?

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

26 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

Related Questions

How do I disable all rigidbodies and colliders in a scene in unity? 2 Answers

Load a GameObject that is outside of script and set it active at the same time 1 Answer

Enable and disabled parent contraint with c# 1 Answer

c# How Do I disable current script or anyother script in the program within code? 2 Answers

using Aim Constraint 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