error cs1041?

I’m currently having a problem with my coding. It states “error cs1041 : Identifier expected, ‘float’ is a keyword”. I’ve tried searching for solutions all over the internet but it seems like my problem stayed the same :


using UnityEngine;

//Material texture offset rate

public class float speed = .5f;

//Offset the material texture at a constant rate

void FixedUpdate () {

float offset = Time.time * speed;

renderer.material.mainTextureOffset = new Vector2(0, -offset);
}


please help, I need the solution as soon as possible. Thanks.

Your code makes no sense. Inside the scope of a name space you can only declare types.

A class declaration basically has the form:

<visibility modifier> class <someIdentifier> [ : <SomeBaseClass>]
{
    // class body
}

where things in brackets are generally optional. However if you try to create a component which can be attached to a gameobject you have to derive your class from “MonoBehaviour”.

<visibility modifier> can be one of the following: public, private, internal. However you usually declare your class public otherwise it can’t be used outside of your scriptfile.

<someIdentifier> is the name of the class you want to declare. For Unity components that class name has to match the filename otherwise it can’t be attached to a gameobject.

So if your file is named “MyScript.cs” (in Unity you will only see “MyScript”) the class name has to be MyScript.

Any variable or method declaration should go into the “class body” (between the pair of curly brackets that follow the class declaration)

So your class should look something like this:

// MyScript.cs
using UnityEngine;

public class MyScript : MonoBehaviour
{
    // some variable declarations
    public float speed = 0.5f
    private Renderer rend;

    // a method declaraion
    void Start()
    {
        rend = GetComponent<Renderer>();
    }
    
    void Update ()
    {
        // method body
        float offset = Time.time * speed;
        rend.material.mainTextureOffset = new Vector2(0, -offset);
    }
}

Note: the “renderer” shortcut-property doesn’t exist anymore. So you have to use GetComponent<Renderer>() to get access to the renderer component on the same gameobject. To avoid searching for the component every frame we search for it once in Start and store the reference in a variable (“rend”).

Also FixedUpdate is only used in conjunction with the physics system. A better name would have been “PhysicsUpdate”. For anything else you should use Update.