• 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
1
Question by robounited · Dec 17, 2012 at 05:49 PM · cs8025

CS8025 Parsing Error C#

Im getting Parsing Error CS8025 on line 4(var endTime : float;). v v v

using UnityEngine;

using System.Collections;

var endTime : float;

var textMesh : GUIText;

public class Cube: MonoBehaviour {

public GameObject Alarm;

 // Use this for initialization
 void Start () {

 }

 // Update is called once per frame
 void Update () {

var timeLeft : int = endTime - Time.time;

     if (timeLeft < 0) timeLeft = 0;
     textMesh.text = timeLeft.ToString();
 }

 void OnTriggerEnter(Collider collider)
 {

Destroy(Alarm);

endTime = Time.time + 60;

textMesh = GameObject.Find ("Timer").GetComponent(GUIText);

textMesh.text = "60";

}

}

Any help ??

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by clunk47 · Dec 17, 2012 at 05:54 PM

You were using a bunch of Javascript defining variables. In C# you have to define variables within the class. It also had a few more issues like variables being defined both via class and locally... I have written and tested this revision with a result of no errors or warnings. Just take this and study it, compare it to your original.

 using UnityEngine;
 using System.Collections;
 
 public class Cube: MonoBehaviour 
 {
     public float endTime;
     public GUIText textMesh;
     public GameObject Alarm;
     float timeLeft;
 
     // Use this for initialization
     void Start () 
     {
     
     }
 
     // Update is called once per frame
     void Update () 
     {
         timeLeft = endTime - Time.time;
         
         if (timeLeft < 0) 
                 timeLeft = 0;
     
         textMesh.text = timeLeft.ToString();
     }
     
     void OnTriggerEnter(Collider collider)
     {
         Destroy(Alarm);
         endTime = Time.time + 60;
         textMesh = GameObject.Find ("Timer").GetComponent<GUIText>();
         textMesh.text = "60";
     }
 
 }
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
avatar image
0

Answer by Karsnen_2 · Dec 17, 2012 at 06:00 PM

You are mixing Javascript with C#. When you use a variable var, you have to declare the variable right away not define the type. The usage "var" in C# is basically for dynamic type-casting hence you need not to do something like this.

 var endTime : float;

But instead what is advisable and recommend to do is, something like this :

 var endTime = 2.4f;


Let me know, if this helped you.

Comment
Add comment · Show 4 · 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
avatar image fafase · Dec 17, 2012 at 06:11 PM 1
Share

var does work in C# but it does not have the meaning of UnityScript. You use it when you want the compiler to deter$$anonymous$$e the type. Nonetheless, this is not appropriate in this question.

avatar image Karsnen_2 · Dec 17, 2012 at 06:50 PM 0
Share

Yeah. Fafase -> I guess that is what I tried to say too. I said he was mixing Javascript with C#. and fafase just to correct myself. With what I have said -> is there anything factual wrong?

avatar image clunk47 · Dec 17, 2012 at 07:27 PM 1
Share

Using var in C# would not work correctly when refering to this question.

avatar image fafase · Dec 17, 2012 at 07:53 PM 1
Share

Well, you explain the surface of the var keyword as there are more to it. It is commonly used with LINQ (the .NET SQL) as you can store pretty much whatever you want in a var variable.

Other example:

 var enemy = new { health = 3,name="villain" };

You create an "object" enemy with two properties. As they are properties they are read-only. and you use them like:

 print(enemy.health);

but you cannot enemy.health = 10;

or again

  var list = new List<int>();
  list.Add(10); list.Add(20);
  foreach (object obj in list) print(obj);

This is call anonymous type.

As I said it is commonly used in LINQ since you can store database requests where you never know how many items will be returned

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

Error in script? 1 Answer

Assets/Scripts/PlayerController.cs(22,25): error CS8025: Parsing error 0 Answers

What Does error mean : i got it from this line of script...... 2 Answers

Unexpected Symbol 'end-of-file' C# Error CS8025 1 Answer

Whats wrong with this script? 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