• 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
-1
Question by PaxStyle · Nov 11, 2015 at 09:22 PM · boxcollideronmousedown

Two script using the same box Collider

Hi to all, I have a light button script, and a change material script (on/off material), both script work with the OnMouseDown function, but the box collider is in the same position for both the script (is the light-button). If I click on the button, only one of the two script work. How can I do to work both script toghether? Thank you in advance ;)

Comment
Add comment · Show 10
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 PaxStyle · Nov 14, 2015 at 11:13 AM -1
Share

Please is important to me.. :/

avatar image instruct9r · Nov 14, 2015 at 02:07 PM 0
Share

I didn't undestand, what happens to the boxCollider, when you press the button???

The object changes material, or the object dissapears?

What do you mean box collider is in the same position for both scripts.... You have 2 scripts on the same object? Or you have 2 objects with boxCOlliders at the same position...

You have to be clear, when explaining your problem, so people can help you.

avatar image kawales · Nov 14, 2015 at 02:12 PM 0
Share

Give us the scripts if you want us to help.

avatar image Le-Pampelmuse · Nov 14, 2015 at 10:24 PM 1
Share

Your scripts contain many errors and are not very efficiently written (of course, since you are a beginner, that's normal).

This forum is not a database of people who do your work. We are here to ask and answer questions for problems that can't be directly answered by Tutorials or Documentation. If you get errors in Unity Editor, google them. Your Light script throws at least 1 error:
error CS1501: No overload for method Get$$anonymous$$ouseButton takes 0 arguments

First, look at the Documentation or Tutorials: http://unity3d.com/learn

If you get no answer there, you can SEARCH here for the question. If you still get no answer yet, then you can post a NEW question here.

By the way, for script commands look at the Scripting API: http://docs.unity3d.com/ScriptReference/index.html

Just some examples:

  • if (Input.Get$$anonymous$$ouseButton) does not take 0 arguments. It takes an int.
    if (Input.Get$$anonymous$$ouseButton(0)) is correct, 0 is the left mouse button, 1 is the right button.

  • var resetTime = 0.0000000000000000; this is completely useless. 0.000000 is exactly the same as 0 or 0.0 or 0.000. Its just zero. ;)

  • ar Animazione = "AnimSbarra"; is also an error, the "v" or var is missing. Of course Unity Editor tells you that.

Please consider the points I stated above before asking a new question.
Don't be afraid to learn on your own. :)
Have a nice day!

avatar image PaxStyle Le-Pampelmuse · Nov 15, 2015 at 08:58 AM -1
Share

I know that my scripts are not completely corrrect, But at least they work, and for start learning this is good to me..(ps. I've removed the Input.Get$$anonymous$$ouseButton, It was useless inside a On$$anonymous$$ouseDown Function). Thank you the same for your suggestions, but my question was different.. How to make work two script that use the same collider.. If someone knows this, I'd glad to hear the solution.

avatar image Le-Pampelmuse · Nov 15, 2015 at 10:35 AM 1
Share

You have still not explained what "did not work" as you want it to. Your question is

How to make work two script that use the same collider

The answer is: function On$$anonymous$$ouseDown(){}

You can add 100, 354 or 2 or whatever number of seperate scripts with On$$anonymous$$ouseDown() to one Collider and each of them will work every time you click on the collider.

Script 1:

 using UnityEngine;
 using System.Collections;
 
 public class b : $$anonymous$$onoBehaviour {

     void On$$anonymous$$ouseDown () {
         print ("B");
     }
 }

Script2:

 using UnityEngine;
 using System.Collections;
 
 public class a : $$anonymous$$onoBehaviour {

     void On$$anonymous$$ouseDown () {
         print ("A");
     }
 }

This will print A and B every time you click on the object. You must look into your code to find the problem. Think about what it does, when clicking, when on == true, when on == false and you find the answer.

If you want people to fix your code, you should not use this forum.

avatar image PaxStyle Le-Pampelmuse · Nov 15, 2015 at 05:36 PM -1
Share

I didn't say this.. $$anonymous$$y script work to me.. I don't need new script. I try to explain the problem better.

For example:

Your script a: make appear a light if collider is pressed.

Your script b: $$anonymous$$ake change the object-material if the collider is pressed.

.................................................

Your script 'a' is attached to a switch, and if you click on it, the light became on.

Your script 'b' is attached to a lamp (so another gameobject) but use the switch collider, and If you click on it the object material should change.

So, both the script work with the same collider (the switch collider), but they don't work together if the collider is pressed. Seems a stupid problem to solve, hope i explained well this time.

avatar image Le-Pampelmuse PaxStyle · Nov 15, 2015 at 09:08 PM 1
Share

Aha! Now we get to the problem. On$$anonymous$$ouseDown, On$$anonymous$$ouseEnter, etc. only work for the collider where it is attached to.

What you would need to do (using a and b as example again):

Script a: attach to switch, use On$$anonymous$$ouseDown to turn on/off light.

Script b: attach to switch, use On$$anonymous$$ouseDown to change materials, BUT have public material reference to the renderer of the lamp object.

Look here: http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.On$$anonymous$$ouseEnter.html

Look at the code and you can use almost the same for your task.

Show more comments
Show more comments

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

34 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

Related Questions

Conflicting Box Colliders 0 Answers

OnMouseDown generates random sprite 1 Answer

Click for destroy Prefab, but sometime don't work 1 Answer

OnMouseDown won't work on a simple button 2 Answers

How to show vertices on a cube when selected? 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