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 Klown · Aug 08, 2013 at 04:59 PM · collisioncollidertoggle

Toggle a script/key on when collide with object.

I'm wanting to disable a script at the start, and then enable it when I run into the set collider item, my script also destroys the collision item at the same time.

I have everything working, except I can't get it to actually toggle the target script.

This is what I'm trying to do...

Disable the mapToggle key "m" until the player walks over the collision area of the map item that's on the ground and hits the key "e" to pick up/destroy the item on the ground. Thus enabling the use of the "m" key to toggle the map item.

  using UnityEngine;
     using System.Collections;
     
     public class pickupkey : MonoBehaviour {
         public Transform WalkedOverObject;
     
         void Update () {
         if(Input.GetKeyDown(KeyCode.E))
        {
         if (pickupItem.WalkedOverObject) Destroy(pickupItem.WalkedOverObject);
             }
         }    
     }
 
Comment
Add comment · Show 6
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 CypherGames · Aug 08, 2013 at 05:48 PM 0
Share

Is the mapToggle script C# or Unityscript?

avatar image Klown · Aug 09, 2013 at 02:29 AM 0
Share

It's C# script.

avatar image Joyrider · Aug 13, 2013 at 05:35 PM 0
Share

what is pickupItem?

avatar image Klown · Aug 13, 2013 at 05:48 PM 0
Share

pickupItem is the script that detects if you are near/on an item that is able to be picked up. by your character.

 using UnityEngine;
 using System.Collections;
  
 public class pickupItem : $$anonymous$$onoBehaviour {
      public static GameObject WalkedOverObject;
  
      void Start() {
      }
  
      void OnTriggerEnter(Collider other) {
           WalkedOverObject = other.gameObject;
 }
      void OnTriggerExit(Collider other) {
           if (WalkedOverObject == this.gameObject) WalkedOverObject = null;
     }
 }
avatar image Joyrider · Aug 13, 2013 at 05:50 PM 0
Share

okay and what script are you trying to enable/disable?

Show more comments

2 Replies

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

Answer by Joyrider · Aug 13, 2013 at 06:02 PM

Okay, than something like this, I guess

I'm not checking if the picked up object is a particular object here yet though. And I'm assuming here, the mapToggle script is on the same object as the pickup script.

 using UnityEngine;
 using System.Collections;
  
 public class pickupkey : MonoBehaviour 
 {
     public GameObject mapObject;
     private mapToggle mapToggleScript;
     void Start()
     {
         mapToggleScript = mapObject.GetComponent<mapToggle>();
         if(mapToggleScript!=null)
         {
              mapObject.renderer.enabled = false; // or mapToggleScript.Start(); if Start is set public
              mapToggleScript.enabled = false;
         }
     }  
     void Update () 
     {
         if(Input.GetKeyDown(KeyCode.E))
         {
             if (pickupItem.WalkedOverObject!=null)   
             {  
                 if(mapToggleScript!=null)
                      mapToggleScript.enabled = true;
                 Destroy(pickupItem.WalkedOverObject);
             }
        }
     }  
 }

Little correction on pickupItem script

 using UnityEngine;
 using System.Collections;
  
 public class pickupItem : MonoBehaviour {
      public static GameObject WalkedOverObject;
  
      void Start() {
      }
  
      void OnTriggerEnter(Collider other) 
      {
           WalkedOverObject = other.gameObject;
      }
      void OnTriggerExit(Collider other) 
      {
           if (WalkedOverObject == other.gameObject) WalkedOverObject = null;
      }
 }
 
Comment
Add comment · Show 9 · 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 Klown · Aug 13, 2013 at 06:10 PM 0
Share

The mapToggle script is on the item that is being toggled, the pickupkey and pickupItem scripts are both on the graphics of the first person controller as to have a collision area detection.

This is what I'm trying to do...

Disable the mapToggle key "m" until the player walks over the collision area of the map item that's on the ground and hits the key "e" to pick up/destroy the item on the ground. Thus enabling the use of the "m" key to toggle the map item.

avatar image Joyrider · Aug 13, 2013 at 06:27 PM 0
Share

Okay, added variable to set the mapObject in, and corrected something line 16 of the pickupItem script, changed this to other.

you could also just make the mapToggleScript variable public and assign it in the inspector by drag'n'dropping the mapObject in that field. Than there would be no need for the newly added mapObjectVariable, nor for the GetComponent

avatar image Klown · Aug 13, 2013 at 06:36 PM 0
Share

This is only doing what I had it doing, it's still not disabling the key "m" or the script "mapToggle".

avatar image Joyrider · Aug 13, 2013 at 06:42 PM 0
Share

It should... that is what mapToggleScript.enabled = false; is for. Did you assign the map object to the mapObject variable in the inspector?

avatar image Klown · Aug 13, 2013 at 06:50 PM 0
Share

I see what you mean. Now I'm running into the issue of the map being visible at the start. That may be another problem because this is the script of the item that's starting disabled but needs one key component to still be working at the void start....the renderer.enable=false; part. this is the coding attached to my toggle map component.

 using UnityEngine;
 using System.Collections;
 
 public class mapToggle : $$anonymous$$onoBehaviour {
     public Character$$anonymous$$otor char$$anonymous$$ot;
  
    void Start () {
         renderer.enabled=false;
         }
  
     void Update () {
     if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$))
        {
         renderer.enabled = !renderer.enabled;
         char$$anonymous$$ot.canControl = !renderer.enabled;
         }
     }
 }
Show more comments
avatar image
0

Answer by CypherGames · Aug 08, 2013 at 07:32 PM

Try this as your script:

 using UnityEngine;
 using System.Collections;
 
 public class pickupItem : MonoBehaviour {
      public Transform WalkedOverObject;
      public mapToggle toggleScript;
 
      void Start() {
           toggleScript = WalkedOverObject.GetComponent<mapToggle>();
           toggleScript.active = false;
      }
 
      void OnTriggerEnter(Collider other) {
           Destroy(other.gameObject);
           toggleScript.active = true;
      }
 }
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 Klown · Aug 09, 2013 at 05:44 PM 0
Share

I tried this, and I updated the coding a bit, but I still can't make it to where the script is disabled first, then after using the "e" command to pick up the item enables the script.

Hmmm

avatar image Klown · Aug 13, 2013 at 05:32 PM 0
Share

Aka...this doesn't work.

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

16 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to make an arrow to become stuck? 1 Answer

Keep particles to defined area, destroy on collision 0 Answers

Collider Error[SOLVED] 2 Answers

Cover mechanic - Restrict movement to box 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