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 CupOfThings · Sep 05, 2014 at 06:03 PM · objectsizeselectshape

Change shape of object if selected?

What I am trying to achieve here is to be able to change the shape and size of an object but only if the object has been selected. My script makes sense to me but does not work. The object can be selected but when I use any of the WASD keys to change its shape nothing happens. Can someone help me out?

     var selected = false;
     var SelectedGameObject : GameObject;
     
     
     function Update (){
     
     if(Input.GetMouseButtonDown(0)){
     
     var hit : RaycastHit;
     var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     
     if (Physics.Raycast (ray, hit, 100.0)){
     SelectedGameObject = hit.collider.gameObject;
     
     selected = true;
     Change();
     Wait(hit.collider.renderer);
     }
     }
     }
     
     function Change (){
     
     if(Input.GetKeyDown(KeyCode.A)){
     transform.localScale += Vector3(0.1,0,0);
     }
 
     if(Input.GetKeyDown(KeyCode.D)){
     transform.localScale += Vector3(-0.1,0,0);
     }
 
     if(Input.GetKeyDown(KeyCode.W)){
     transform.localScale += Vector3(0,0,0.1);
     }
 
     if(Input.GetKeyDown(KeyCode.S)){
     transform.localScale += Vector3(0,0,-0.1);
     }
     }
     
     function Wait(rend : Renderer){
     
     var originalColor = rend.material.color;
     rend.material.color = Color.yellow;
     yield WaitForSeconds(2);
     rend.material.color = originalColor;
     }
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
Best Answer

Answer by dsada · Sep 05, 2014 at 06:18 PM

The problem is that your Change function is inside an Input.GetMouseButtonDown() condition. The only possible way to change the scale of your object if you manage to click and push the button in the same frame. So nearly impossible. What i suggest is something like this:

 function Update (){
  
     if(Input.GetMouseButtonDown(0)){
  
     var hit : RaycastHit;
     var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
  
     if (Physics.Raycast (ray, hit, 100.0)){
     SelectedGameObject = hit.collider.gameObject;
  
     selected = true;
     Wait(hit.collider.renderer);
     }
     }
 
 
     if(selected)
     {
        Change();
     }
  }
Comment
Add comment · Show 3 · 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 CupOfThings · Sep 05, 2014 at 06:36 PM 0
Share

Thanks works perfectly! Is there a simple way to deselect the selected object if another object has been selected ins$$anonymous$$d?

avatar image dsada · Sep 05, 2014 at 07:22 PM 0
Share

Well, of course it is pretty easy if you only want to do this.

store a GameObject variable in this class and store there the selected object. in the raycasthit check if the clicked object is the selected one and if it is, then do whatever you want to deselect it. But it pretty much depends on the further plans with this script. But let me sketch an easy solution.

 public class SomeClass
 {
 
 var selected : GameObject;
 
 function Update (){
  
     if(Input.Get$$anonymous$$ouseButtonDown(0)){
  
     var hit : RaycastHit;
     var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
  
     if (Physics.Raycast (ray, hit, 100.0)){
         SelectedGameObject = hit.collider.gameObject;
  
         if(selected != null && hit.gameObject == selected)
         {
            selected = null;
         }
         else
         {
            selected = hit.gameObject;
         }
     }
     }
  
  
     Change();
  }
 
 
 function Change (){
  
     if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A)){
     selected.transform.localScale += Vector3(0.1,0,0);
     }
  
     if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.D)){
     selected.transform.localScale += Vector3(-0.1,0,0);
     }
  
     if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W)){
     selected.transform.localScale += Vector3(0,0,0.1);
     }
  
     if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.S)){
     selected.transform.localScale += Vector3(0,0,-0.1);
     }
     }
 }


Please forgive me if there are syntax errors i never code in javascript. But thepoint is understandable i guess

avatar image CupOfThings · Sep 05, 2014 at 10:27 PM 0
Share

I have opened a new question for this. Thanks for the help.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Deselect a selected object? 2 Answers

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

Destroy only selected object 1 Answer

Help with hiding an object on trigger enter 1 Answer

Trouble grabbing and holding a cube. 0 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