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 serenefox · Mar 09, 2013 at 04:58 AM · javascripttriggerdisable

How to disable a script with OnTriggerEnter?

I am trying to disable two scripts I have on two different game objects after a collision enters a trigger. I am not sure how to set it up becasue I keep getting errors for everything I try. Thanks for any advice and your time. Here is my script:

 #pragma strict

 var cameraFollow : CameraFollow;
 var planeSelfMove : PlaneSelfMove;
 
 function OnTriggerEnter( other : Collider )
 {    
     
     if(other.gameObject.tag == "CameraTrigger")
     {
         cameraFollow = gameObject.GetComponent(CameraFollow).enabled = false;
         planeSelfMove = gameObject.GetComponent(PlaneSelfMove).enabled = false;
     }
 }


Edit: I have changed my script to this (below) thanks to sharpshot124 because well it makes sense but now I get this null reference error: NullReferenceException: Object reference not set to an instance of an object StopCamera.OnTriggerEnter (UnityEngine.Collider collider) line 13

Line 13 is the cameraFollow.enabled = false;

So I am going into detail about my problem(which I should have done first). Here is what I am trying to do:

I am making a top-down shooter and the camera is in the orthographic view, I have "camera collision" around the edges so the player can't go outside the camera's view. I am using the top(top of the screen) camera collision to act as the collision for triggers to do things like spawn enemies and other things. And that works totally fine. But I get all the way through my level to the area where the boss fight is and I want to stop the camera from moving (it has a script attached to it that makes it move at a certain speed constantly). I changed my script to the necessities and to what sharpshot124 suggested. The game plays but when the top camera collision enters that trigger nothing happens and I get that error. The top camera collision does have a rigidbody so thats fine I believe. Also I'm attaching the script to the top camera collider. The trigger I am trying to get to work is named "SpawntriggerStopCamera". Can anyone shed some light as to why I get that error? Again here is the revised script:

    #pragma strict
 
     var cameraFollow : CameraFollow;
     
     function OnTriggerEnter( collider : Collider )
     {    
         
         if(collider.gameObject.name == "SpawntriggerStopCamera")
         {
             cameraFollow = gameObject.GetComponent(CameraFollow);
                     cameraFollow.enabled = false;
         }
     }
Comment
Add comment · Show 1
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 robertbu · Mar 09, 2013 at 05:10 AM 0
Share

What errors (post error message from the Console if possible), and on what line.

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Kleptomaniac · Mar 09, 2013 at 11:07 PM

Nobody here is giving you a clear answer.

I would set out your script like so:

 #pragma strict

 var topdownCam : GameObject;
 
 private var cameraFollow: CameraFollow;
 private var planeSelfMove: PlaneSelfMove;
 
 function Start () {
      
      cameraFollow = topdownCam.GetComponent(CameraFollow);
      planeSelfMove = GetComponent(PlaneSelfMove);
 
 }
 
 function OnTriggerEnter (collider : Collider) {
     
      if (collider.game object.tag == "SpawntriggerStopCamera") {
           cameraFollow.enabled = false;
           planeSelfMove.enabled = false;
      }
 }

I declared the script component references so that it wouldn't slow your code each time when creating a new component reference. :)

Hope that helps,

Klep


EDIT:

Now all you have to do is assign your TopCameraCollision object to the topdownCam variable in the Inspector, and you're good to go! :)

Comment
Add comment · Show 5 · 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 serenefox · Mar 10, 2013 at 12:22 AM 0
Share

Thanks for the reply but I still get the Null Reference Exception with the "cameraFollow.enabled = false;" line. I don't know if this information will make any difference but when I click on the error in the log, the "TopCameraCollision" Game Object gets highlighted.

avatar image Kleptomaniac · Mar 10, 2013 at 12:38 AM 0
Share

Ah right, so your CameraFollow script is attached to your TopCameraCollision GameObject? O$$anonymous$$, I'll edit my script above to fix this.

avatar image serenefox · Mar 10, 2013 at 02:20 AM 0
Share

Actually my cameraFollow is attacted to my camera. But I will try messing with the script fix and change it to the camera.

avatar image serenefox · Mar 10, 2013 at 02:23 AM 0
Share

Yup it worked thank you so much

avatar image Kleptomaniac · Mar 10, 2013 at 02:38 AM 0
Share

No worries, glad I could help! :)

avatar image
0

Answer by sharpshot124 · Mar 09, 2013 at 05:38 AM

lines 11 and 12 are invalid. u cannot have 2 statements on one line change it to this

 cameraFollow = gameObject.GetComponent(CameraFollow);
 cameraFollow.enabled = false;
 planeSelfMove = gameObject.GetComponent(PlaneSelfMove);
 planeSelfMove.enabled = false;

also make sure that one of the objects has a non trigger collider and one object has a rigidbody, otherwise the function wont be called at all.

Comment
Add comment · Show 1 · 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 serenefox · Mar 09, 2013 at 08:22 AM 0
Share

For your suggestion i get this error: BCE0020: An instance of type 'UnityEngine.Behaviour' is required to access non static member 'enabled'.

avatar image
0

Answer by 3dws · Mar 09, 2013 at 06:41 AM

hi script is a Component . u must access to your script component to disable it

u can access it in 2 way ;

1 : if script attached to external object u must use this code

     gameobject.find("objectname").getcomponent(scriptname).enable=false;
 

2 : if u want to disable component that attached to object by itself , u can use this code

     transform.getcomponent(scraiptname).enable=false;
 



Comment
Add comment · Show 1 · 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 serenefox · Mar 09, 2013 at 08:11 AM 0
Share

With your suggestion i get this error: BCE0019: 'enable' is not a member of 'CameraFollow'. BCE0019: 'enable' is not a member of 'PlaneSelf$$anonymous$$ove'.

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

13 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

Related Questions

collider trigger to disable/enable script 1 Answer

How to Disable/Enable another Script with Triggers? 1 Answer

Get a UserTrigger in Javascript - Eg for SmoothMoves 1 Answer

What’s wrong with my Safe Volume (safe zone) code? 0 Answers

how do I disable a script 3 Answers


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