Search Unity

  1. New Unity Live Help updates. Check them out here!

    Dismiss Notice

Operator '*' cannot be used with a left hand side of type...

Discussion in 'iOS and tvOS' started by ShaneTheVeryTall, Dec 30, 2009.

  1. ShaneTheVeryTall

    ShaneTheVeryTall

    Joined:
    Nov 28, 2009
    Posts:
    11
    I'm trying to get an enemy to move towards the hero and adjust the speed. For some reason, this code works in Unity, but not Unity iPhone:

    Code (csharp):
    1.  
    2. var speed : float = 4;
    3. private var moveSPeed : float;
    4.  
    5. function LateUpdate() {
    6.  
    7.      distance = Vector3.Distance( player.transform.position, transform.position);
    8.  
    9.      if ( distance < 15  ) {
    10.         //Player is close  
    11.            moveSpeed = speed * Time.deltaTime;
    12.                ChasePlayer(moveSpeed);
    13.     }
    14. }
    15.  
    16. function ChasePlayer(moveSpeed) {
    17.    // (object is already rotated/facing towards player via another script)
    18.     transform.Translate(Vector3.forward * moveSpeed); // ERROR HAPPENS HERE
    19. }
    20.  
    I get the error:
    Operator '*' cannot be used with a left hand side of type 'UnityEngine.Vector3' and a right hand side of type 'Object'.

    so if I change that line so that instead of multiplying by moveSpeed it multiplies by 2 or Time.deltaTime(for example), it works without throwing an error.

    Any idea what's up?

    Thanks in advance!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,225
    Code (csharp):
    1. function ChasePlayer(moveSpeed : float) {
    --Eric
     
  3. ShaneTheVeryTall

    ShaneTheVeryTall

    Joined:
    Nov 28, 2009
    Posts:
    11
    ahh... I didn't realize I had to re-type the variable, but I guess that makes sense, since it's kind of like a new variable in that function.

    Thank You!!!
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,225
    Indeed, it is a variable local to the function.

    --Eric
     
unityunity