Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question error CS1002: ; expected but I have a ; at the line?

Discussion in 'Scripting' started by Loading7088, May 5, 2023.

  1. Loading7088

    Loading7088

    Joined:
    May 5, 2023
    Posts:
    9
    Hey, so writing my first script in my first game, I get the error above at 19,36, 19,37, and 19,54... I also get error CS1003 , expected. Below is my code.
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Baby : MonoBehaviour
    7. {
    8.  
    9.     public float moveSpeed = 4;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         gameObject.tag = "Baby";
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         transform.Translate vector3.left * moveSpeed * Time.deltaTime;
    21.     }
    22. }
    23.  
     
  2. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    497
    Bunny83 and halley like this.
  3. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,363
    transform.Translate(moveSpeed * Time.deltaTime * Vector3.left);


    The Vector3.left also needed to match in upper/lowercase.

    Also, it's a good practice to put float * float * vector, rather than vector * float * float, even though they mathematically come out the same.
     
  4. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    is there any reason for that or just convention?
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,314
    Please use the scripting forum for scripting questions, not the 2D forum.

    I'll move your post for you.
     
  6. Nefisto

    Nefisto

    Joined:
    Sep 17, 2014
    Posts:
    329
    the amount of multiplications is smaller in the first case
     
  7. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,912
    Exactly. When you do
    Vector3 * float * float
    multiplication is carried out left to right. So you get
    (Vector3 * float) * float


    That means you have a vector * scaler multplication first which results in a new vector which is then again multiplied by a scaler. A total of 6 float * float multiplications in this case

    When you do
    float * float * Vector3
    it's also carried out left to right so you get this
    (float * float) * Vector3


    Here you first multiply those two float together and then you do the scaler * Vector3 multiplication. This results in a total of 4 float-float multiplications.

    Instead of changing the order you can also use brackets to combine the scalers first:
    Vector3 * (float * float)


    This effect of course scales when you have multiple factors like

    Code (CSharp):
    1. Vector3.left * speed * slowDownFactor *  0.5f * Time.deltaTime
    Here we have 12 float-float multiplications because we have 4 vector-scaler multiplications. When you first combine the scalers (3 float-float multiplications) and then doing the vector-scaler (again 3 float-float) we only did 6 in total.

    Of course in case of using those cardinal direction constants, using this would be even better:

    Code (CSharp):
    1. new Vector3(-speed * slowDownFactor *  0.5f * Time.deltaTime, 0, 0);
    Though it looks a bit messier.

    Those are not really a big deal but it makes more sense conceptionally. imagine you have 3 buckets which you want to fill with a color that you have to mix from 4 separate colors. Of course it makes more sense to first mix the 4 colors and then just put the result in each of the buckets instead of taking the first color, put some in each bucket, then taking the second color and add a bit to each bucket and so on. Not the best analogy but I hope you get the point :)