Search Unity

Problem with variables in c#

Discussion in 'Scripting' started by half_voxel, Apr 2, 2008.

  1. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    Hi.
    I have some trouble with my c# code i get an error (expecting ";".) when writing this.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. //using System.IO;
    5. public class txtprinter : MonoBehaviour {
    6.  
    7.     // Use this for initialization
    8.     void Awake () {
    9.         public string hello = "hello";
    10.         //public string label;
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     /*void Printing () {   
    15.         TextWriter objWriter= new StreamWriter("hej.txt");
    16.         //objWriter.WriteLine(label);
    17.         objWriter.Flush();
    18.         objWriter.Close();
    19.         objWriter=null;
    20.         print ("hej");
    21.     }*/
    22. }
    23.  
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    In the Awake method, you have used the "public" modifier, but you can't declare a local variable as public. If you want to add a property to the class then you have to declare it outside of any method body. If you want a local variable then just leave out the word "public". If you are initialising the variable then write "varName = value;" rather than using a declaration.
     
  3. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    OK thanks. That will help me a lot :D