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 soulblade · Oct 01, 2012 at 10:11 AM · javascriptcollisiononmousedown

help with script

hi everyone i just need some help with my script. its supposed to make a pickup that can only be picked up when it has collided with the player and the mouse button has been pushed how ever it dosent work. here is the script attached to the player:\

 function OnTriggerEnter(other : Collider){
    if (other.CompareTag("Paper")) {
       this.GetComponent(destroyandcall).enabled=false;
    }
 }
 
 function OnTriggerExit(other : Collider){
    if (other.CompareTag("Paper")) {
       this.GetComponent(destroyandcall).enabled=true;
    }
 }

and here is the script on the pickup:

 var batteryPower : int = 1; //The amount of points to give, negative value takes away points instead
 
 function OnMouseDown () {
     notecount.AlterEnergy(batteryPower);
     Destroy (gameObject);
 }

please answer if you can thanks.

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 whydoidoit · Oct 01, 2012 at 10:27 AM 0
Share

Non- sleeping rigidbody on at least one of the things involved with the Trigger?

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ZenithCode · Oct 01, 2012 at 10:25 AM

Which part of the script does not work?
Can you do some debugs?

I believe this does not work on JS: this.GetComponent(destroyandcall).enabled=false;

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 soulblade · Oct 01, 2012 at 11:29 AM 0
Share

it works its just not enabling it true or false

avatar image ZenithCode · Oct 01, 2012 at 11:39 AM 0
Share

So what you need to do is DestroyComponent and re-adding it when you need it.

$$anonymous$$g Remove: Destroy(this.GetComponent(destroyandcall));

Add: this.AddComponent(destroyandcall);

avatar image soulblade · Oct 05, 2012 at 06:18 AM 0
Share

thx for your help

avatar image soulblade · Oct 05, 2012 at 06:27 AM 0
Share

and this.AddComponent(destroyandcall); does not work?

avatar image whydoidoit · Oct 05, 2012 at 06:33 AM 0
Share

There is no need to Add and Destroy components to enable them.

avatar image
0

Answer by whydoidoit · Oct 05, 2012 at 06:33 AM

Your problem is that you are doing a GetComponent on the Player and not on the pickup.

Your code should look like this:

 function OnTriggerEnter(other : Collider){
    if (other.CompareTag("Paper")) {
       other.GetComponent(destroyandcall).enabled=false;
    }
 }
 
 function OnTriggerExit(other : Collider){
    if (other.CompareTag("Paper")) {
       other.GetComponent(destroyandcall).enabled=true;
    }
 }
Comment
Add comment · 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
0

Answer by soulblade · Oct 05, 2012 at 07:31 AM

too late I had a brainstorm earlier and i found the answer myself. I removed the script on the player and added a sphere under the pickup, renderd the sphere invisible and set it to is trigger. then I added the players script to the sphere changed it to this:

 var ranOnce : boolean = false;
 
 function OnTriggerEnter(other : Collider){
    if (other.CompareTag("Player"))
     if (!ranOnce)
     {
         ranOnce = true;
         transform.root.gameObject.AddComponent(destroyandcall);
    }
 }
 
 function OnTriggerExit(other : Collider){
    if (other.CompareTag("Player")) {
       Destroy(transform.root.gameObject.GetComponent(destroyandcall));
    }
 }

and it works perfectly!

Comment
Add comment · 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

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

11 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

Related Questions

OnCollisionStay for seconds then destroy 0 Answers

Destroy a GameObject 2 Answers

Overlap Detection HELP!! 0 Answers

Change scene with trigger collision not working. 1 Answer

Collsion without ririgibody 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