• 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 Gusic · Jul 23, 2015 at 02:29 PM · c#vector2mouseclick

why am i getting a vector2.scale error

Hi I have a script which works when i hold the mouse down the player expands in the x axis and when i let go it returns to normal. But i keep getting the error Operator *' cannot be applied to operands of type method group' and `float'

CODE

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour
 {
     public float speed = 10;
     private Vector3 originalScale;
     
     void Start()
     {
         originalScale = transform.localScale;
     }
     
     void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             transform.localScale = transform.localScale + Vector2.Scale * speed * Time.deltaTime;
             new Vector2(5.545318f, 0.5769206f);
         }
         
         if (Input.GetMouseButtonUp(0))
         {
             transform.localScale = originalScale;
         }
     }
 }

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
1

Answer by Bunny83 · Jul 23, 2015 at 02:43 PM

Looks like you want:

 transform.localScale += (Vector3)(Vector2.one * speed * Time.deltaTime);
 // or
 transform.localScale += new Vector3(1f,1f,0f) * speed * Time.deltaTime;


Keep in mind that this will only add to the x and y scale and keep the z scale as it is.

edit

Oh, another thing. Since you use "Input.GetMouseButtonDown" you either have to remove Time.deltaTime since you don't execute this every frame. So it makes no sense to use deltaTime here.

Or replace "GetMouseButtonDown" with "GetMouseButton" to execute the code inside the if every frame as long as you hold down the mouse button. I guess this is what you actually want since the first thing doesn't make much sense.

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 Gusic · Jul 23, 2015 at 07:07 PM 0
Share

it tells me The call is ambiguous between the following methods or properties: UnityEngine.Vector2.operator +(UnityEngine.Vector2, UnityEngine.Vector2)' and UnityEngine.Vector3.operator +(UnityEngine.Vector3, UnityEngine.Vector3)'

avatar image Bunny83 · Jul 24, 2015 at 05:21 PM 0
Share

@Gusic: Ah yes, that's why implicit conversion operators are a pain. Vector3 can be implicitly converted to Vector2 and also reverse. So if you bring a V3 together with a V2 the compiler doesn't know if it should convert the V3 to V2 and do the addition or the V2 to a V3 and do the addition. I'll edit my answer ^^.

avatar image
0

Answer by kemar · Jul 23, 2015 at 02:38 PM

Hi,

You are trying to multiplie a method with a float. The prototype of Vector2.Scale is

 Vector2 vec1;
 Vector2 vec2;
 
 Vector2.Scale(vec1, vec2);

Go to the doc ;) http://docs.unity3d.com/ScriptReference/Vector2.Scale.html

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 Gusic · Jul 23, 2015 at 02:40 PM 0
Share

what would i have to replace it with to scale up in x axis

avatar image kemar · Jul 23, 2015 at 02:52 PM 1
Share

you can try this

     float scaleX = transform.localScale.x + transform.localScale.x * speed * Time.deltaTime;
     transform.localScale = new Vector3(scaleX, transform.localScale.y, transform.localScale.z);

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

23 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# Convert Vector3[] to Vector2[] 3 Answers

Shift-Click on Scene View to add Vector2 0 Answers

Click On object that has a larger collider 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