Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 AndreasLybo · Jul 16, 2016 at 01:27 PM · scripting problemscriptingbasics

Using sliders to control X and Z of an object

I'm trying to control an objects X and Z using 2 sliders (1 for each value). It worked fine using the script for X, but when using the script for Z nothing happens. Both are applied to the object and both "AdjustWidth" and "AdjustHeight" are added to the slider value.

Code for X:

   using UnityEngine;
   using System.Collections;
   public class Gulv_bredde : MonoBehaviour
   {
 
     // Use this for initialization
     public float gulvBredde = 1f;
     void Start()
     {
 
     }
     public void AdjustWidth(float nyBredde)
     {
         gulvBredde = nyBredde;
     }
     // Update is called once per frame
     void Update()
     {
         transform.localScale = new Vector3(1f+gulvBredde, 0.25f, 1f);
     }
 
 }

Code for Y:

   using UnityEngine;
   using System.Collections;
   public class GulvLengde : MonoBehaviour
   {
 
     // Use this for initialization
     public float gulvLengde = 1f;
     void Start()
     {
 
     }
     public void AdjustHeight(float nyLengde)
     {
         gulvLengde = nyLengde;
     }
     // Update is called once per frame
     void Update()
     {
         transform.localScale = new Vector3(1f, 0.25f, 1f+gulvLengde);
     }
 
 }
 

Any help is appreciated!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by AndreasLybo · Jul 16, 2016 at 03:13 PM

I changed the code to this and it now works :)

         Z:
         Vector3 scale = transform.localScale;
         scale.z = gulvLengde;
         transform.localScale = scale;

         X:
         Vector3 scale = transform.localScale;
         scale.x = gulvBredde;
         transform.localScale = scale;
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 wojtask12 · Jul 16, 2016 at 02:59 PM

Note 1: use English in your code, it'll make your life easier. trust me. Note 2: Your approach is really bad, you should avoid calling Update() function if it's not necessary. Note 3: You're trying to do 2 things every frames:

  transform.localScale = new Vector3(1f, 0.25f, 1f+gulvLengde);

and

  transform.localScale = new Vector3(1f+gulvBredde, 0.25f, 1f);

First operation sets your object's scale to, let's say (1,0.25,1.53) (Z value dependent on slider value) Second operation takes this rescaled object and it rescales it again - in this point, it sets Z scale back to 1 - so the output scale is (1+sliderValue,0.25,1). You need to have it this way:

 float xScale, zScale;
 Transform objectToResize;
 public void RescaleX(float sliderValue)
 {
     xScale = sliderValue;
     ResizeObject();
 }
 public void RescaleZ(float sliderValue)
 {
     zScale= sliderValue;
     ResizeObject();
 }
 void ResizeObject()
 {
     objectToResize.localScale = new Vector3(xScale, 0.25f, zScale);
 }

Assign RescaleX and RescaleZ to your sliders, and it's done.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can i check and fire an event when the user look at specific object ? 0 Answers

Adding callbacks/events to an instantiated prefab 2 Answers

How do I inherit certain varibles and functions from another script. 1 Answer

building error in my video project apk 0 Answers

Running scripts from editor 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