• 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 cadviz · Sep 08, 2012 at 10:32 PM · javascriptonmousedown

Is it possible to call a script using onMouseDown function

Hi

Is it possible to call a script using onMouseDown function?

Comment
Add comment · Show 5
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 cadviz · Sep 08, 2012 at 09:44 PM 0
Share

If not..!!

Is it possible to select an object using on$$anonymous$$ouseDown function?

avatar image Piflik · Sep 08, 2012 at 11:18 PM 1
Share

Can you be more specific? What do you want to do?

You can do a lot of things On$$anonymous$$ouseDown...

avatar image cadviz · Sep 09, 2012 at 04:06 AM 0
Share

I have a scene with 3 objects, and a script to create 3 GUI buttons for 3 materials, that can be used for assigning a material to a selected object...

What I need is while I'm in play mode I should be able to:

1-select any of the objects by mouse.

2-change the material of the selected object through the 3 GUI buttons created previously by the script.

$$anonymous$$y idea is to call the script each time I select any of the 3 objects.

Am I wrong...!!?

avatar image Cold_Ankles · Sep 09, 2012 at 04:21 AM 0
Share

To clarify:

You want to click an object which causes a script to run that causes 3 buttons to appear that, when clicked change the material of the clicked object.

Is that right? I'm fairly sure that's 100% possible, I'm just not sure how to do it though...

avatar image cadviz · Sep 09, 2012 at 04:31 AM 0
Share

Yes Cold_Ankles; that is right, this is what I like to achieve.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Piflik · Sep 09, 2012 at 10:08 AM

What I would do is wrap the code for the GUI in an if statement, controlled by a boolean variable. When you click on one of the objects, you set that variable to 'true' and the GUI should appear.

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 cadviz · Sep 09, 2012 at 05:14 PM 0
Share

@Philipp Thanks for your trying to help...

does your suggestion work with "On$$anonymous$$ouseDown" function?

if your answer is "yes", then how to select an object by clicking it in play mode?

sorry for bothering... I am newbie with JavaScript.

avatar image cadviz · Sep 09, 2012 at 09:31 PM 0
Share

Here is the code for the script attached to the 3 objects, but it doesn't work properly... may be somebody can give an advice...

// This script can be used to create buttons

//on the screen to change Object's $$anonymous$$aterials.

//Attach it the object you want to change materials

var button1_tex : Texture;

var button2_tex : Texture;

var button3_tex : Texture;

var button4_tex : Texture;

var crate_tex1 : Texture;

var crate_tex2 : Texture;

var crate_tex3 : Texture;

var crate_tex4 : Texture;

var _mouseOver = false;

 var _mouseClick = false;


function On$$anonymous$$ouseOver() {

 _mouseOver = true;

}

function On$$anonymous$$ouseExit() {

 _mouseOver = false;

}

function On$$anonymous$$ouseDown () {

if(_mouseOver){

    _mouseClick=true;

}

}

function OnGUI(){

 if(_mouseClick) {


  if ( GUI.Button (Rect (100,570,40,40),button1_tex) )

renderer.material.mainTexture = crate_tex1;

else

if( GUI.Button (Rect (175,570,40,40), button2_tex) )

renderer.material.mainTexture = crate_tex2;

else

if( GUI.Button (Rect (250,570,40,40), button3_tex) )

renderer.material.mainTexture = crate_tex3;

else

if( GUI.Button (Rect (325,570,40,40), button4_tex) )

renderer.material.mainTexture = crate_tex4;

 }


}

avatar image
0

Answer by kristercollin · Sep 09, 2012 at 10:00 PM

You were very close, Cadviz, regardless of what your problem was.

I've taken the code you posted above and modified only the OnGUI section, this should be close to what you were looking to do, at least.

 function OnGUI()
 {
     if(_mouseClick)
     {
         if (GUI.Button (Rect (100,570,40,40), "Button 1") )
         {
             Debug.Log("Texture 1 should come up");
             renderer.material.mainTexture = crate_tex1;
             _mouseClick = false; //This part of the code hides the buttons away
         }
 
         if (GUI.Button (Rect (175,570,40,40), "Button 2") )
         {
             Debug.Log("Texture 2 should come up");
             renderer.material.mainTexture = crate_tex2;
             _mouseClick = false; //This part of the code hides the buttons away
         }
 
     }
 }

So long as you have assigned the textures in the variable slots of the gameobject using the inspector, it should work just fine.

Let us know how you go =)

Comment
Add comment · Show 4 · 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 cadviz · Sep 10, 2012 at 04:45 AM 0
Share

@kristercollin

First if all...thanks a lot for your help;

I had used your GUI section in my script above, because I had 4 materials, and run the game...here is what happened:

1- I clicked an object to select

2- 4 buttons appeared

3- I clicked one of the GUI buttons to change the selected object's material, it worked fine, but

4- the 4 buttons disappeared

5- I clicked another object to select

6- The 4 buttons appeared again

7- I clicked another GUI button

8- the 4 buttons disappeared again

So, It is fine except one thing,that is each time I Clicked any of the GUI buttons, they disappeared.

I tried to deactivate the part that hides the buttons away to keep the 4 buttons on screen,

The result is the buttons never disappeared,but they affect only one of the 3 objects...

Here is your code I used with my edit


function OnGUI()

{

 if(_mouseClick)

 {

    if (GUI.Button (Rect (100,570,40,40), button1_tex) )

    {

      Debug.Log("Texture 1 should come up");

      renderer.material.mainTexture = crate_tex1;

      _mouseClick = false; //This part of the code hides the buttons away

    }



    if (GUI.Button (Rect (175,570,40,40), button2_tex) )

    {

      Debug.Log("Texture 2 should come up");

      renderer.material.mainTexture = crate_tex2;

      _mouseClick = false; //This part of the code hides the buttons away

    }



if ( GUI.Button (Rect (250,570,40,40), button3_tex) )

    {        

Debug.Log("Texture 3 should come up");

renderer.material.mainTexture = crate_tex3;

      _mouseClick = false; //This part of the code hides the buttons away

    }       

 

if ( GUI.Button (Rect (325,570,40,40), button4_tex) )

    {

Debug.Log("Texture 4 should come up");

renderer.material.mainTexture = crate_tex4;

      _mouseClick = false; //This part of the code hides the buttons away

    }

 }

}

avatar image kristercollin · Sep 10, 2012 at 06:01 AM 0
Share

Sorry, I just assumed you'd want the buttons to disappear once you're done changing the texture.

If you don't want them to disappear, just remove this line of code:

 _mouseClick = false;

from the if statements and they'll stay.

However, remember that the buttons are all co$$anonymous$$g up in the same place (you placed them using literal co-ordinates, not relative co-ordinates), so they will stack on top of each other. That means you'll only have access to the buttons for one object at a time. If you want to fix this, you can add something to the script which finds the object's position in space and then tells the buttons to appear a distance from that space.

for example, doing something like this:

 myPositionX = transform.position.x - 20;
 myPositionY = transform.position.y - 20;
 
        if (GUI.Button (Rect (myPositionX, myPositionY,40,40), button1_tex) )
        {}

What this code does is gets the position of the object, then set a new position for button that's relative to the gameobject it belongs to. This is a simple and very limited example, but you can use this technique to make it work.

avatar image cadviz · Sep 10, 2012 at 06:58 AM 0
Share

I see... But I would rather to position buttons in the same area and same place, Why?

Well,in fact I'm an architect and wish to present my projects in 3D environment with real_time visualization just like PC Games... to show my clients many proposals & alternatives for walls, floor materials etc... in a quick way...

An idea jumps in $$anonymous$$d now; is it possible

when I click a gameobject, it's buttons comes up to be used, then in case I click another gameobject the old buttons disappear and new buttons comes up those are related with the second clicked gameobject, and so on for the rest gameobjects in the scene..!!

$$anonymous$$y post here is just the beginning, and still have many problems to solve using JavaScript which I am newbie with...

I had watched this

http://vimeo.com/34073227

Here is my first post

http://answers.unity3d.com/questions/313821/how-to-change-an-object-material-using-gui-buttons.html

Thanks a lot for trying to help

Regards

avatar image kristercollin · Sep 10, 2012 at 11:24 PM 0
Share

"I see... But I would rather to position buttons in the same area and same place, Why?"

The way that the GUI buttons work is that they are positioned on a 2D plane which sits in front of the 3D simulation that is happening. If you make buttons appear at the exact same co-ordinates with the exact same size on this 2D plane, then only one of the buttons that is stacked in this position will be accessible, as all the other ones will be considered as being behind it. That's why I suggested you find a way to place them on screen dynamically. You can learn much more about that here and here.

"wish to present my projects in 3D environment with real_time visualization ... An idea jumps in $$anonymous$$d now; is it possible"

The code we've worked on so far shows a strong promise for it being possible, though there are many larger topics you'll have to contend with as it goes on.

"when I click a gameobject, it's buttons comes up to be used, then in case I click another gameobject the old buttons disappear and new buttons comes up"

I thought as such and that's how I had it set up in the original answer that I gave. It literally worked EXACTLY as you have stated here that you wanted it to. Feel free to try the code again and see.

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

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

OnMouseDown on what? Camera Movement Help. 1 Answer

Load scene when right click button is released not working 1 Answer

Prefabs and OnMouseDown (Javascript) 1 Answer

Show/hide GUI Texture 1 Answer

help with 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