V
V
Vladislav Kapralov2019-03-09 12:12:30
C++ / C#
Vladislav Kapralov, 2019-03-09 12:12:30

How to fix code in Unity?

Wrote a code that makes the object move smoothly horizontally "back and forth".
And this case is far from professional.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NailBoard : MonoBehaviour {
    
    public GameObject nail;
    public float moveto; //смещение на... 
    private bool toright = false; // Направлено в право?

  void Start () 
  {
    
  }
  
  void Update () 
  {
    if (toright == false)
    {
            nail.transform.position.x + (moveto * Time.DeltaTime);
            toright = true;
        }
        else
        {
        	nail.transform.position.x - (moveto * Time.DeltaTime);
            toright = false;
        }
  }
}

Unity throws an error:
Assets/Scripts/NailBoard.cs(20,28): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a
statement

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2019-03-09
@Zorexo

It's not hard to describe what's wrong with you.
You add two numbers (in your case, the offset and the current position), but you don't write down the total value of this addition anywhere. c# doesn't like that. You need to write down the result somewhere, obviously in a position.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question