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 Jasper1900 · Mar 19, 2013 at 04:19 PM · javascriptguiinstantiateraycastgui button

How to activate a button?

Hi there,

I'm having a problem with my script.. It's written in javascript and I can't figure out what's wrong. I want to have a GUI Button and if I click on it I should be able to place a object in the scene. The instantiating part works fine, with a raycast. But if I add a GUI Button, it instantiates at the moment I click, and only if I click the button, so you can only place object in the button part of the screen..

Is there any way to script it so that you can instantiate if you clicked the button once, and if you click it again, you can't?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by roojerry · Mar 19, 2013 at 04:27 PM

 bool clickedOnce = false; //global variable for that script
 
 //if GUI button clicked
 if(!clickedOnce){
    //Instantiate object
    clickedOnce = true;
 }
Comment
Add comment · Show 6 · 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 Jasper1900 · Mar 19, 2013 at 04:48 PM 0
Share

So this is what I have right now, but it still only instantiates on the button, if I want it outside the button, it does nothing.

pragma strict

var buildingThatWillBeBuild : GameObject;

function OnGUI() {

var clickedOnce : boolean = false;

if(!clickedOnce){

if (GUI.Button(Rect(10,70,50,30),"Build")) {

 var ray : Vector3 = Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x,Input.mousePosition.y, 15));
         Debug.DrawRay (transform.position, ray, Color.green);
 
 
         if(Input.Get$$anonymous$$ouseButtonUp(0))                                            
         {    
             var rayRay = Camera.main.ScreenPointToRay (Input.mousePosition);
             var hit : RaycastHit;
             
             if (Physics.Raycast (rayRay, hit)) {
             
             if(hit.collider.gameObject.tag =="Ground")
             {
                                                                                     
             Instantiate (buildingThatWillBeBuild, hit.point,     buildingThatWillBeBuild.transform.rotation);
             clickedOnce = true;    
             
                 }
             }
         }    
     }
 }

}

avatar image roojerry · Mar 19, 2013 at 04:53 PM 0
Share

Are you saying that you want to click the button, then click again to place the building where you want it?

avatar image Jasper1900 · Mar 19, 2013 at 04:54 PM 0
Share

Exactly, ;)

avatar image roojerry · Mar 19, 2013 at 05:04 PM 0
Share
 var buildingThatWillBeBuild : GameObject;
     
     var clickedOnce : boolean = false;
     var canBuild : boolean = false;
     
     function OnGUI() {
     
         if (GUI.Button(Rect(10,70,50,30),"Build")) {
               if(!clickedOnce){
                  canBuild =true;
               }
            }
        }
     
     function Update(){
     
         var ray : Vector3 =         Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x,Input.mousePosition.y, 15));
         Debug.DrawRay (transform.position, ray, Color.green);
         
         
         if(canBuild && Input.Get$$anonymous$$ouseButtonUp(0))                            
         { 
              var rayRay = Camera.main.ScreenPointToRay (Input.mousePosition);
              var hit : RaycastHit;
             
              if (Physics.Raycast (rayRay, hit)) {
             
                  if(hit.collider.gameObject.tag =="Ground")
                  {
                 
                      Instantiate (buildingThatWillBeBuild, hit.point,     buildingThatWillBeBuild.transform.rotation);
                      clickedOnce = true;  
                      canBuild = false;
                   }
              }
         }
     }



I wrote this pretty quickly, but it should be close to what you want

avatar image Jasper1900 · Mar 19, 2013 at 05:14 PM 0
Share

I get your way of thinking, but it does exactly the same as my script. It only creates a object in the button part of the screen

Show more comments

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

A way to instantiate an item with 90 Degree increments? 2 Answers

Changing variable in a script of a game object hit with raycast 1 Answer

[SOLVED] Only instantiating once 1 Answer

[Logical Error] Select a game object and perform actions using GUI.Button 1 Answer

Setting Scroll View Width GUILayout 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