Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Myra_Z · Jul 04, 2017 at 02:49 AM · scripting problemscript.scripting beginner

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Jul 04, 2017 at 03:23 AM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

106 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Slowly increase motor.force 1 Answer

How do I make a photography function? 0 Answers

Show speed in UI 1 Answer

what to change in the script that it calls start from a external button other then when collider is detected 1 Answer

Why when using get; set; in one script i'm getting null in other script that use it ? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges