Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Variables

Discussion in 'Scripting' started by Dext, May 30, 2014.

  1. Dext

    Dext

    Joined:
    May 29, 2014
    Posts:
    10
    Hi guys,

    I have a little problem with my CSharp Code in Unity

    Code (csharp):
    1. [CODE]using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Sphere : MonoBehaviour {
    5.  
    6.     public Vector3 startposition;
    7.    
    8.     public GameObject Patikelsystem;
    9.     public GameObject Spring;
    10.    
    11.      int hallo = 30;
    12.     int hallo1 = 70;
    13.    
    14.    
    15.    
    16.     // Use this for initialization
    17.    
    18.     void Start () {
    19.        
    20.        
    21.        
    22.        
    23.         startposition = transform.position ;
    24.        
    25.        
    26.        
    27.        
    28.     }
    29.    
    30.    
    31.    
    32.     // Update is called once per frame
    33.    
    34.     void Update () {
    35.  
    36.         if(Input.GetKey(KeyCode.N))
    37.        
    38.             int hallo = 300 ;
    39.  
    40.  
    41.  
    42.  
    43.  
    44.  
    45.  
    46.  
    47.        
    48.         Spring.transform.position = new Vector3 ( transform.position.x, transform.position.y, transform.position.z) ;
    49.        
    50.        
    51.         Patikelsystem.transform.position = new Vector3 ( transform.position.x, transform.position.y, transform.position.z) ;
    52.        
    53.  
    54.            
    55.            
    56.            
    57.  
    58.        
    59.        
    60.         if(transform.position.y <= 2)
    61.         {
    62.        
    63.             if(Input.GetKeyDown(KeyCode.L))
    64.                
    65.            
    66.                
    67.                
    68.          rigidbody.AddForce (0,300,0);
    69.                
    70.             }
    71.            
    72.        
    73.        
    74.        
    75.        
    76.        
    77.        
    78.         if(transform.position.y <= -10 )
    79.         {
    80.            
    81.            
    82.            
    83.             transform.position = startposition;
    84.            
    85.            
    86.            
    87.             rigidbody.angularVelocity = Vector3.zero;
    88.            
    89.            
    90.            
    91.             rigidbody.velocity = Vector3.zero;
    92.            
    93.            
    94.            
    95.            
    96.            
    97.            
    98.            
    99.            
    100.            
    101.             return;
    102.         }
    103.        
    104.        
    105.        
    106.         if(Input.GetKey(KeyCode.UpArrow ) )
    107.            
    108.            
    109.            
    110.             rigidbody.AddTorque(hallo1*Time.deltaTime,0,0);
    111.        
    112.        
    113.        
    114.         if(Input.GetKey(KeyCode.DownArrow))
    115.            
    116.            
    117.            
    118.             rigidbody.AddTorque(-hallo*Time.deltaTime,0,0);
    119.        
    120.        
    121.        
    122.         if(Input.GetKey(KeyCode.LeftArrow))
    123.            
    124.            
    125.            
    126.             rigidbody.AddTorque(0,0,hallo*Time.deltaTime);
    127.        
    128.        
    129.        
    130.        
    131.        
    132.         if(Input.GetKey(KeyCode.RightArrow))
    133.            
    134.            
    135.            
    136.             rigidbody.AddTorque(0,0,-hallo*Time.deltaTime);
    137.        
    138.        
    139.        
    140.        
    141.        
    142.        
    143.        
    144.        
    145.        
    146.        
    147.        
    148.        
    149.        
    150.        
    151.        
    152.        
    153.        
    154.        
    155.        
    156.     }
    157.    
    158. }
    159.  
    [/CODE][/CODE]

    I have 2 variables, and this two variables controls some numbers in terms of the rigidbody rotation. Now I want to change them, if I press a certain button. But Unity saiys:

    Assets/Scripts/Sphere.cs(38,41): error CS1023: An embedded statement may not be a declaration or labeled statement.

    The second question I have is (when this problem is fixed], how can I say Unity with the help of CSharp that this key has already been pressed and it is no more allowed to use this key again. Because this is a one time ultimate skill.
     
  2. Tiki

    Tiki

    Joined:
    Mar 3, 2013
    Posts:
    299
    You've already declared hallo. On line 38, you can simply put "hallo = 300;", as the program is already aware it's an int due to line 11.
     
  3. Dext

    Dext

    Joined:
    May 29, 2014
    Posts:
    10
    Thank you worked for me. Could you please answer my second question and how I can connect that with a delay, more or less that the normal speed comes back after 3 seconds, that would be very helpful
     
    Last edited: May 30, 2014
  4. sootie8

    sootie8

    Joined:
    Mar 25, 2014
    Posts:
    233
  5. StaticNova

    StaticNova

    Joined:
    Feb 23, 2013
    Posts:
    60
    You were declaring the hallo variable twice, I've fixed that in the following code and added a flag to the N key input so it can only be pressed once. Hope this helps. :)

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Sphere : MonoBehaviour {
    5.     public Vector3 startposition;
    6.     public GameObject Patikelsystem;
    7.     public GameObject Spring;
    8.    
    9.     int hallo = 30;
    10.     int hallo1 = 70;
    11.  
    12.     bool halloToggled;  // Prevents the hallo button toggle being pressed twice.
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.         startposition = transform.position ;
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update () {
    21.  
    22.         if(Input.GetKey(KeyCode.N)  halloToggled = false) {
    23.             halloToggled = true;
    24.             hallo = 300;
    25.         }
    26.  
    27.         Spring.transform.position = new Vector3 ( transform.position.x, transform.position.y, transform.position.z) ;
    28.  
    29.         Patikelsystem.transform.position = new Vector3 ( transform.position.x, transform.position.y, transform.position.z) ;
    30.  
    31.         if(transform.position.y <= 2)
    32.         {
    33.             if(Input.GetKeyDown(KeyCode.L)) {
    34.                 rigidbody.AddForce (0,300,0);
    35.             }
    36.         }
    37.  
    38.         if(transform.position.y <= -10 )
    39.         {
    40.             transform.position = startposition;
    41.             rigidbody.angularVelocity = Vector3.zero;
    42.             rigidbody.velocity = Vector3.zero;
    43.  
    44.             return;
    45.         }
    46.  
    47.         if(Input.GetKey(KeyCode.UpArrow ) ) {
    48.             rigidbody.AddTorque(hallo1*Time.deltaTime,0,0);
    49.         }
    50.  
    51.         if(Input.GetKey(KeyCode.DownArrow)) {
    52.             rigidbody.AddTorque(-hallo*Time.deltaTime,0,0);
    53.         }
    54.  
    55.         if(Input.GetKey(KeyCode.LeftArrow)) {
    56.             rigidbody.AddTorque(0,0,hallo*Time.deltaTime);
    57.         }
    58.  
    59.         if(Input.GetKey(KeyCode.RightArrow)) {
    60.             rigidbody.AddTorque(0,0,-hallo*Time.deltaTime);
    61.         }
    62.     }
    63. }
     
  6. Dext

    Dext

    Joined:
    May 29, 2014
    Posts:
    10
    Thank you, is it possible that you can add to that script a delay, because I have no clue how i shall do this with a coroutine. ( after 4 seconds should hallo = 30 and hallo1 = 70 again. Could you show me how I can make that.

    If I use you script, Unity says:



    Assets/Scripts/Sphere.cs(67,52): error CS1519: Unexpected symbol `(' in class, struct, or interface member declaration

    Assets/Scripts/Sphere.cs(65,54): error CS1519: Unexpected symbol `)' in class, struct, or interface member declaration

    If I use the old script everthing is fine

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. using System.Collections;
    4.  
    5.  
    6.  
    7. public class Sphere : MonoBehaviour {
    8.    
    9.     public Vector3 startposition;
    10.    
    11.     public GameObject Patikelsystem;
    12.    
    13.     public GameObject Spring;
    14.    
    15.    
    16.    
    17.     int hallo = 30;
    18.    
    19.     int hallo1 = 70;
    20.    
    21.    
    22.    
    23.     bool halloToggled;  // Prevents the hallo button toggle being pressed twice.
    24.    
    25.    
    26.    
    27.     // Use this for initialization
    28.    
    29.     void Start () {
    30.        
    31.         startposition = transform.position ;
    32.        
    33.     }
    34.    
    35.    
    36.    
    37.     // Update is called once per frame
    38.    
    39.     void Update () {
    40.        
    41.        
    42.        
    43.         if(Input.GetKey(KeyCode.N)  halloToggled = false) {
    44.            
    45.             halloToggled = true;
    46.            
    47.             hallo = 300;
    48.            
    49.         }
    50.        
    51.        
    52.        
    53.         Spring.transform.position = new Vector3 ( transform.position.x, transform.position.y, transform.position.z) ;
    54.        
    55.        
    56.        
    57.         Patikelsystem.transform.position = new Vector3 ( transform.position.x, transform.position.y, transform.position.z) ;
    58.        
    59.        
    60.        
    61.         if(transform.position.y <= 2)
    62.            
    63.         {
    64.            
    65.             if(Input.GetKeyDown(KeyCode.L)) {
    66.                
    67.                 rigidbody.AddForce (0,300,0);
    68.                
    69.             }
    70.            
    71.         }
    72.        
    73.        
    74.        
    75.         if(transform.position.y <= -10 )
    76.            
    77.         {
    78.            
    79.             transform.position = startposition;
    80.            
    81.             rigidbody.angularVelocity = Vector3.zero;
    82.            
    83.             rigidbody.velocity = Vector3.zero;
    84.            
    85.            
    86.            
    87.             return;
    88.            
    89.         }
    90.        
    91.        
    92.        
    93.         if(Input.GetKey(KeyCode.UpArrow ) ) {
    94.            
    95.             rigidbody.AddTorque(hallo1*Time.deltaTime,0,0);
    96.            
    97.         }
    98.        
    99.        
    100.        
    101.         if(Input.GetKey(KeyCode.DownArrow)) {
    102.            
    103.             rigidbody.AddTorque(-hallo*Time.deltaTime,0,0);
    104.            
    105.         }
    106.        
    107.        
    108.        
    109.         if(Input.GetKey(KeyCode.LeftArrow)) {
    110.            
    111.             rigidbody.AddTorque(0,0,hallo*Time.deltaTime);
    112.            
    113.         }
    114.        
    115.        
    116.        
    117.         if(Input.GetKey(KeyCode.RightArrow)) {
    118.            
    119.             rigidbody.AddTorque(0,0,-hallo*Time.deltaTime);
    120.            
    121.         }
    122.        
    123.     }
    124.    
    125. }
     
    Last edited: May 30, 2014
  7. StaticNova

    StaticNova

    Joined:
    Feb 23, 2013
    Posts:
    60
    Woops I forgot to add an extra = in the If, and I've added a coroutine that delays input 4 seconds after you press the N key, if you want it for any of the other keys simply put the halloToggled flag in their if statements and call InputDelay.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Sphere : MonoBehaviour {
    5.  
    6.     public Vector3 startposition;
    7.     public GameObject Patikelsystem;
    8.     public GameObject Spring;
    9.  
    10.     int hallo = 30;
    11.     int hallo1 = 70;
    12.     bool halloToggled;  // Prevents the hallo button toggle being pressed twice.
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.         startposition = transform.position ;
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update () {
    21.  
    22.         if(Input.GetKey(KeyCode.N)  halloToggled == false) {
    23.             halloToggled = true;
    24.             hallo = 300;
    25.             StartCoroutine("InputDelay");
    26.         }
    27.  
    28.         Spring.transform.position = new Vector3 ( transform.position.x, transform.position.y, transform.position.z) ;
    29.         Patikelsystem.transform.position = new Vector3 ( transform.position.x, transform.position.y, transform.position.z) ;
    30.  
    31.         if(transform.position.y <= 2)
    32.         {
    33.             if(Input.GetKeyDown(KeyCode.L)) {
    34.                 rigidbody.AddForce (0,300,0);
    35.             }
    36.         }
    37.  
    38.         if(transform.position.y <= -10 )
    39.         {
    40.             transform.position = startposition;
    41.             rigidbody.angularVelocity = Vector3.zero;
    42.             rigidbody.velocity = Vector3.zero;
    43.             return;
    44.         }
    45.  
    46.         if(Input.GetKey(KeyCode.UpArrow ) ) {
    47.             rigidbody.AddTorque(hallo1*Time.deltaTime,0,0);
    48.         }
    49.  
    50.         if(Input.GetKey(KeyCode.DownArrow)) {
    51.             rigidbody.AddTorque(-hallo*Time.deltaTime,0,0);
    52.         }
    53.  
    54.         if(Input.GetKey(KeyCode.LeftArrow)) {
    55.             rigidbody.AddTorque(0,0,hallo*Time.deltaTime);
    56.         }
    57.  
    58.         if(Input.GetKey(KeyCode.RightArrow)) {
    59.             rigidbody.AddTorque(0,0,-hallo*Time.deltaTime);
    60.         }
    61.     }
    62.  
    63.     float delay = 4.00f;
    64.     IEnumerator InputDelay() {
    65.         yield return new WaitForSeconds(delay);
    66.         halloToggled = false;
    67.         hallo = 30;
    68.     }
    69. }
     
  8. Dext

    Dext

    Joined:
    May 29, 2014
    Posts:
    10
    Thnak you it worked without problems. Could i ask you another question.

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. using System.Collections;
    4.  
    5.  
    6.  
    7.  
    8.  
    9. public class Kegel : MonoBehaviour {
    10.  
    11.     public GameObject Kamera;
    12.    
    13.    
    14.     public Vector3 startposition;
    15.    
    16.     public GameObject Patikelsystem;
    17.    
    18.     public GameObject Spring;
    19.     public GameObject  Guishow; // that is the object that represents the GUIText
    20.    
    21.    
    22.    
    23.     int hallo = 10;
    24.    
    25.     int hallo1 = 10;
    26.    
    27.     bool halloToggled;  // Prevents the hallo button toggle being pressed twice.
    28.    
    29.    
    30.    
    31.     // Use this for initialization
    32.    
    33.     void Start () {
    34.        
    35.         startposition = transform.position ;
    36.        
    37.     }
    38.    
    39.    
    40.    
    41.     // Update is called once per frame
    42.  
    43.  
    44.    
    45.     void Update () {
    46.  
    47.  
    48.        
    49.        
    50.        
    51.         if(Input.GetKey(KeyCode.M)  halloToggled == false) {
    52.            
    53.             halloToggled = true;
    54.            
    55.             hallo = 1;
    56.             hallo1 = 1;
    57.            
    58.             StartCoroutine("InputDelay");
    59.            
    60.         }
    61.        
    62.        
    63.        
    64.         Spring.transform.position = new Vector3 ( transform.position.x, transform.position.y, transform.position.z) ;
    65.        
    66.         Patikelsystem.transform.position = new Vector3 ( transform.position.x, transform.position.y, transform.position.z) ;
    67.        
    68.        
    69.        
    70.         if(transform.position.y <= 2)
    71.            
    72.         {
    73.            
    74.             if(Input.GetKeyDown(KeyCode.Space)) {
    75.                
    76.                 rigidbody.AddForce (0,300,0);
    77.                
    78.             }
    79.            
    80.         }
    81.        
    82.        
    83.        
    84.         if(transform.position.y <= -10 )
    85.            
    86.         {
    87.            
    88.             transform.position = startposition;
    89.            
    90.             rigidbody.angularVelocity = Vector3.zero;
    91.            
    92.             rigidbody.velocity = Vector3.zero;
    93.            
    94.             return;
    95.            
    96.         }
    97.        
    98.        
    99.        
    100.         if(Input.GetKey(KeyCode.W ) ) {
    101.            
    102.             rigidbody.AddTorque(hallo1,0,0);
    103.            
    104.         }
    105.        
    106.        
    107.        
    108.         if(Input.GetKey(KeyCode.S)) {
    109.            
    110.             rigidbody.AddTorque(-hallo,0,0);
    111.            
    112.         }
    113.        
    114.        
    115.        
    116.         if(Input.GetKey(KeyCode.A)) {
    117.            
    118.             rigidbody.AddTorque(0,0,hallo);
    119.            
    120.         }
    121.        
    122.        
    123.        
    124.         if(Input.GetKey(KeyCode.D)) {
    125.            
    126.             rigidbody.AddTorque(0,0,-hallo);
    127.            
    128.         }
    129.        
    130.     }
    131.    
    132.    
    133.    
    134.     float delay = 5.00f;
    135.    
    136.     IEnumerator InputDelay() {
    137.        
    138.         yield return new WaitForSeconds(delay);
    139.        
    140.         halloToggled = false;
    141.        
    142.         hallo = 10;
    143.         hallo1 = 10;
    144.        
    145.     }
    146.    
    147. }
    I implemented a GUI text Gameobject. But I don´t know how I can address it. I want the GUIShow to count +1 if the Sphere is under .y -10 (that is the part I already scripted.). But i don´t know how to combine.
     
  9. StaticNova

    StaticNova

    Joined:
    Feb 23, 2013
    Posts:
    60
    If you define GUIShow as a GUIText object you can directly access it's text. Best way would be to have a variable that stores what you want to print out in GUIShow for example:

    Code (csharp):
    1.  
    2. var guiCount: int = 0;
    3. GUIShow.text = guiCount++;
    4.  
    You'll want to put the second line in the if statement that determines if the y is below -10.
     
  10. Dext

    Dext

    Joined:
    May 29, 2014
    Posts:
    10
    Did not worked as expected

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. using System.Collections;
    4.  
    5.  
    6.  
    7.  
    8.  
    9. public class Kegel : MonoBehaviour {
    10.  
    11.     public GameObject Kamera;
    12.    
    13.    
    14.     public Vector3 startposition;
    15.    
    16.     public GameObject Patikelsystem;
    17.    
    18.     public GameObject Spring;
    19.     public GameObject Guishow;
    20.  
    21.  
    22.  
    23.  
    24.  
    25.    
    26.  
    27.    
    28.     int hallo = 10;
    29.    
    30.     int hallo1 = 10;
    31.    
    32.     bool halloToggled;  // Prevents the hallo button toggle being pressed twice.
    33.    
    34.    
    35.    
    36.     // Use this for initialization
    37.    
    38.     void Start () {
    39.        
    40.         startposition = transform.position ;
    41.        
    42.     }
    43.    
    44.    
    45.    
    46.     // Update is called once per frame
    47.  
    48.  
    49.  
    50.    
    51.     void Update () {
    52.  
    53.  
    54.  
    55.         var guiCount: int = 0;
    56.        
    57.        
    58.        
    59.         if(Input.GetKey(KeyCode.M)  halloToggled == false) {
    60.            
    61.             halloToggled = true;
    62.            
    63.             hallo = 1;
    64.             hallo1 = 1;
    65.            
    66.             StartCoroutine("InputDelay");
    67.            
    68.         }
    69.        
    70.        
    71.        
    72.         Spring.transform.position = new Vector3 ( transform.position.x, transform.position.y, transform.position.z) ;
    73.        
    74.         Patikelsystem.transform.position = new Vector3 ( transform.position.x, transform.position.y, transform.position.z) ;
    75.        
    76.        
    77.        
    78.         if(transform.position.y <= 2)
    79.            
    80.         {
    81.            
    82.             if(Input.GetKeyDown(KeyCode.Space)) {
    83.                
    84.                 rigidbody.AddForce (0,300,0);
    85.                
    86.             }
    87.            
    88.         }
    89.        
    90.        
    91.        
    92.         if(transform.position.y <= -10 )
    93.            
    94.         {
    95.            
    96.             transform.position = startposition;
    97.            
    98.             rigidbody.angularVelocity = Vector3.zero;
    99.            
    100.             rigidbody.velocity = Vector3.zero;
    101.  
    102.             GUIShow.text = guiCount++;
    103.            
    104.             return;
    105.            
    106.         }
    107.        
    108.        
    109.        
    110.         if(Input.GetKey(KeyCode.W ) ) {
    111.            
    112.             rigidbody.AddTorque(hallo1,0,0);
    113.            
    114.         }
    115.        
    116.        
    117.        
    118.         if(Input.GetKey(KeyCode.S)) {
    119.            
    120.             rigidbody.AddTorque(-hallo,0,0);
    121.            
    122.         }
    123.        
    124.        
    125.        
    126.         if(Input.GetKey(KeyCode.A)) {
    127.            
    128.             rigidbody.AddTorque(0,0,hallo);
    129.            
    130.         }
    131.        
    132.        
    133.        
    134.         if(Input.GetKey(KeyCode.D)) {
    135.            
    136.             rigidbody.AddTorque(0,0,-hallo);
    137.            
    138.         }
    139.        
    140.     }
    141.    
    142.    
    143.    
    144.     float delay = 5.00f;
    145.    
    146.     IEnumerator InputDelay() {
    147.        
    148.         yield return new WaitForSeconds(delay);
    149.        
    150.         halloToggled = false;
    151.        
    152.         hallo = 10;
    153.         hallo1 = 10;
    154.        
    155.     }
    156.    
    157. }
    following problems occured:

    Assets/Scripts/Kegel.cs(55,29): error CS1525: Unexpected symbol `:', expecting `)', `,', `;', `[', or `='

    Do you know how to fix this ?

    PS: I wrote public Gameobject Guishow not public Gameobject GUIShow maybe you have overlooked that.
     
    Last edited: May 31, 2014
  11. StaticNova

    StaticNova

    Joined:
    Feb 23, 2013
    Posts:
    60
    You'll want to move the declaration of guiCount out of Update, and it will need to be in C# syntax.

    Code (csharp):
    1. int guiCount = 0;
    Also you'll need to change GUIShow.text to Guishow.text, and update the type of it to GUIText.