Search Unity

create variables in method update()

Discussion in 'Scripting' started by stety2, Aug 28, 2012.

  1. stety2

    stety2

    Joined:
    Aug 17, 2012
    Posts:
    58
    I need advice on how I can create a variable in the update ()?
    In Javascripts I would do the following:

    function Update ()
    {
    var abc : int;
    }

    How to do it in C Sharp?
    I tried it:
    void Update()
    {
    public int abc;
    }
    but it does not work: (
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Public makes no sense in methods, so just use:
    Code (csharp):
    1. int abc;
    public defines the "visibility" of variables/methods, ... . As the variable only exists within the Update method, it can not be accesses from outside and thus you can't define its visibility.
     
  3. Tanel

    Tanel

    Joined:
    Aug 31, 2011
    Posts:
    508
    If you declare it inside a method it only lives in that method. So no need for public or private.

    Code (csharp):
    1.  
    2. void Update()
    3. {
    4.    int a;
    5. }
    6.  
     
  4. stety2

    stety2

    Joined:
    Aug 17, 2012
    Posts:
    58
    Tkank you.