[C#] Rotating an object, increasing the speed over time.

As the title says, I want to rotate a GameObject, stating at 0 speed, then slowly speeding up over time.

I have this code so far, and I think I am on the right lines, but I get the error:

Assets/Scripts/rotate.cs(7,41): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `rotate.speed'

My code:

using UnityEngine;
using System.Collections;

public class rotate : MonoBehaviour {

    public float speed = 0f;
    public float incrementedspeed = speed + Time.deltaTime;

    void Update () {
        transform.Rotate(Vector2.right * incrementedspeed * Time.deltaTime);
    }
}

it’s complaining about the incremetedspeed definition being a calculation… really you should be setting the new speed inside the update function (although speed += Time.deltaTime; might be more suitable and then just use speed in the rotate function…)

2 Likes

It works!