Variables

Hi guys,

I have a little problem with my CSharp Code in Unity

[CODE]using UnityEngine;
using System.Collections;

public class Sphere : MonoBehaviour {

	public Vector3 startposition;
	
	public GameObject Patikelsystem;
	public GameObject Spring; 
	
	 int hallo = 30;
	int hallo1 = 70;
	
	
	
	// Use this for initialization
	
	void Start () {
		
		
		
		
		startposition = transform.position ; 
		
		
		
		
	}
	
	
	
	// Update is called once per frame
	
	void Update () {

		if(Input.GetKey(KeyCode.N))
		
			int hallo = 300 ;








		
		Spring.transform.position = new Vector3 ( transform.position.x, transform.position.y, transform.position.z) ;
		
		
		Patikelsystem.transform.position = new Vector3 ( transform.position.x, transform.position.y, transform.position.z) ;
		

			
			
			

		
		
		if(transform.position.y <= 2)
		{
		
			if(Input.GetKeyDown(KeyCode.L))
				
			
				
				
		 rigidbody.AddForce (0,300,0);
				
			}
			
		
		
		
		
		
		
		if(transform.position.y <= -10 )
		{
			
			
			
			transform.position = startposition;
			
			
			
			rigidbody.angularVelocity = Vector3.zero;
			
			
			
			rigidbody.velocity = Vector3.zero;
			
			
			
			
			
			
			
			
			
			return;
		}
		
		
		
		if(Input.GetKey(KeyCode.UpArrow ) )
			
			
			
			rigidbody.AddTorque(hallo1*Time.deltaTime,0,0);
		
		
		
		if(Input.GetKey(KeyCode.DownArrow))
			
			
			
			rigidbody.AddTorque(-hallo*Time.deltaTime,0,0);
		
		
		
		if(Input.GetKey(KeyCode.LeftArrow))
			
			
			
			rigidbody.AddTorque(0,0,hallo*Time.deltaTime);
		
		
		
		
		
		if(Input.GetKey(KeyCode.RightArrow))
			
			
			
			rigidbody.AddTorque(0,0,-hallo*Time.deltaTime);
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	}
	
}

[/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.

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.

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

Unity - Manual: Coroutines Use a coroutine and a use a bool.

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. :slight_smile:

using UnityEngine;
using System.Collections;

public class Sphere : MonoBehaviour {
    public Vector3 startposition;
    public GameObject Patikelsystem;
    public GameObject Spring; 
    
    int hallo = 30;
    int hallo1 = 70;

    bool halloToggled;  // Prevents the hallo button toggle being pressed twice.

    // Use this for initialization
    void Start () {
        startposition = transform.position ; 
    }

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

        if(Input.GetKey(KeyCode.N)  halloToggled = false) {
            halloToggled = true;
            hallo = 300;
        }

        Spring.transform.position = new Vector3 ( transform.position.x, transform.position.y, transform.position.z) ;

        Patikelsystem.transform.position = new Vector3 ( transform.position.x, transform.position.y, transform.position.z) ;

        if(transform.position.y <= 2)
        {
            if(Input.GetKeyDown(KeyCode.L)) {
                rigidbody.AddForce (0,300,0);
            }
        }

        if(transform.position.y <= -10 )
        {
            transform.position = startposition;
            rigidbody.angularVelocity = Vector3.zero;
            rigidbody.velocity = Vector3.zero;

            return;
        }

        if(Input.GetKey(KeyCode.UpArrow ) ) {
            rigidbody.AddTorque(hallo1*Time.deltaTime,0,0);
        }

        if(Input.GetKey(KeyCode.DownArrow)) {
            rigidbody.AddTorque(-hallo*Time.deltaTime,0,0);
        }

        if(Input.GetKey(KeyCode.LeftArrow)) {
            rigidbody.AddTorque(0,0,hallo*Time.deltaTime);
        }

        if(Input.GetKey(KeyCode.RightArrow)) {
            rigidbody.AddTorque(0,0,-hallo*Time.deltaTime);
        }
    }
}

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

using UnityEngine;

using System.Collections;



public class Sphere : MonoBehaviour {
	
	public Vector3 startposition;
	
	public GameObject Patikelsystem;
	
	public GameObject Spring; 
	
	
	
	int hallo = 30;
	
	int hallo1 = 70;
	
	
	
	bool halloToggled;  // Prevents the hallo button toggle being pressed twice.
	
	
	
	// Use this for initialization
	
	void Start () {
		
		startposition = transform.position ; 
		
	}
	
	
	
	// Update is called once per frame
	
	void Update () {
		
		
		
		if(Input.GetKey(KeyCode.N)  halloToggled = false) {
			
			halloToggled = true;
			
			hallo = 300;
			
		}
		
		
		
		Spring.transform.position = new Vector3 ( transform.position.x, transform.position.y, transform.position.z) ;
		
		
		
		Patikelsystem.transform.position = new Vector3 ( transform.position.x, transform.position.y, transform.position.z) ;
		
		
		
		if(transform.position.y <= 2)
			
		{
			
			if(Input.GetKeyDown(KeyCode.L)) {
				
				rigidbody.AddForce (0,300,0);
				
			}
			
		}
		
		
		
		if(transform.position.y <= -10 )
			
		{
			
			transform.position = startposition;
			
			rigidbody.angularVelocity = Vector3.zero;
			
			rigidbody.velocity = Vector3.zero;
			
			
			
			return;
			
		}
		
		
		
		if(Input.GetKey(KeyCode.UpArrow ) ) {
			
			rigidbody.AddTorque(hallo1*Time.deltaTime,0,0);
			
		}
		
		
		
		if(Input.GetKey(KeyCode.DownArrow)) {
			
			rigidbody.AddTorque(-hallo*Time.deltaTime,0,0);
			
		}
		
		
		
		if(Input.GetKey(KeyCode.LeftArrow)) {
			
			rigidbody.AddTorque(0,0,hallo*Time.deltaTime);
			
		}
		
		
		
		if(Input.GetKey(KeyCode.RightArrow)) {
			
			rigidbody.AddTorque(0,0,-hallo*Time.deltaTime);
			
		}
		
	}
	
}

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.

using UnityEngine;
using System.Collections;

public class Sphere : MonoBehaviour {

    public Vector3 startposition;
    public GameObject Patikelsystem;
    public GameObject Spring; 

    int hallo = 30;
    int hallo1 = 70;
    bool halloToggled;  // Prevents the hallo button toggle being pressed twice.

    // Use this for initialization
    void Start () {
        startposition = transform.position ; 
    }

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

        if(Input.GetKey(KeyCode.N)  halloToggled == false) {
            halloToggled = true;
            hallo = 300;
            StartCoroutine("InputDelay");
        }

        Spring.transform.position = new Vector3 ( transform.position.x, transform.position.y, transform.position.z) ;
        Patikelsystem.transform.position = new Vector3 ( transform.position.x, transform.position.y, transform.position.z) ;

        if(transform.position.y <= 2)
        {
            if(Input.GetKeyDown(KeyCode.L)) {
                rigidbody.AddForce (0,300,0);
            }
        }

        if(transform.position.y <= -10 )
        {
            transform.position = startposition;
            rigidbody.angularVelocity = Vector3.zero;
            rigidbody.velocity = Vector3.zero;
            return;
        } 

        if(Input.GetKey(KeyCode.UpArrow ) ) {
            rigidbody.AddTorque(hallo1*Time.deltaTime,0,0);
        } 

        if(Input.GetKey(KeyCode.DownArrow)) {
            rigidbody.AddTorque(-hallo*Time.deltaTime,0,0);
        } 

        if(Input.GetKey(KeyCode.LeftArrow)) {
            rigidbody.AddTorque(0,0,hallo*Time.deltaTime);
        } 

        if(Input.GetKey(KeyCode.RightArrow)) {
            rigidbody.AddTorque(0,0,-hallo*Time.deltaTime);
        }
    }

    float delay = 4.00f;
    IEnumerator InputDelay() {
        yield return new WaitForSeconds(delay);
        halloToggled = false;
        hallo = 30;
    }
}

Thnak you it worked without problems. Could i ask you another question.

using UnityEngine;

using System.Collections;





public class Kegel : MonoBehaviour {

	public GameObject Kamera;
	
	
	public Vector3 startposition;
	
	public GameObject Patikelsystem;
	
	public GameObject Spring; 
	public GameObject  Guishow; // that is the object that represents the GUIText 
	
	
	
	int hallo = 10;
	
	int hallo1 = 10;
	
	bool halloToggled;  // Prevents the hallo button toggle being pressed twice.
	
	
	
	// Use this for initialization
	
	void Start () {
		
		startposition = transform.position ; 
		
	}
	
	
	
	// Update is called once per frame


	
	void Update () {


		
		
		
		if(Input.GetKey(KeyCode.M)  halloToggled == false) {
			
			halloToggled = true;
			
			hallo = 1;
			hallo1 = 1;
			
			StartCoroutine("InputDelay");
			
		}
		
		
		
		Spring.transform.position = new Vector3 ( transform.position.x, transform.position.y, transform.position.z) ;
		
		Patikelsystem.transform.position = new Vector3 ( transform.position.x, transform.position.y, transform.position.z) ;
		
		
		
		if(transform.position.y <= 2)
			
		{
			
			if(Input.GetKeyDown(KeyCode.Space)) {
				
				rigidbody.AddForce (0,300,0);
				
			}
			
		}
		
		
		
		if(transform.position.y <= -10 )
			
		{
			
			transform.position = startposition;
			
			rigidbody.angularVelocity = Vector3.zero;
			
			rigidbody.velocity = Vector3.zero;
			
			return;
			
		} 
		
		
		
		if(Input.GetKey(KeyCode.W ) ) {
			
			rigidbody.AddTorque(hallo1,0,0);
			
		} 
		
		
		
		if(Input.GetKey(KeyCode.S)) {
			
			rigidbody.AddTorque(-hallo,0,0);
			
		} 
		
		
		
		if(Input.GetKey(KeyCode.A)) {
			
			rigidbody.AddTorque(0,0,hallo);
			
		} 
		
		
		
		if(Input.GetKey(KeyCode.D)) {
			
			rigidbody.AddTorque(0,0,-hallo);
			
		}
		
	}
	
	
	
	float delay = 5.00f;
	
	IEnumerator InputDelay() {
		
		yield return new WaitForSeconds(delay);
		
		halloToggled = false;
		
		hallo = 10;
		hallo1 = 10;
		
	}
	
}

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.

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:

var guiCount: int = 0;
GUIShow.text = guiCount++;

You’ll want to put the second line in the if statement that determines if the y is below -10.

Did not worked as expected

using UnityEngine;

using System.Collections;





public class Kegel : MonoBehaviour {

	public GameObject Kamera;
	
	
	public Vector3 startposition;
	
	public GameObject Patikelsystem;
	
	public GameObject Spring; 
	public GameObject Guishow;





	

	
	int hallo = 10;
	
	int hallo1 = 10;
	
	bool halloToggled;  // Prevents the hallo button toggle being pressed twice.
	
	
	
	// Use this for initialization
	
	void Start () {
		
		startposition = transform.position ; 
		
	}
	
	
	
	// Update is called once per frame



	
	void Update () {



		var guiCount: int = 0;
		
		
		
		if(Input.GetKey(KeyCode.M)  halloToggled == false) {
			
			halloToggled = true;
			
			hallo = 1;
			hallo1 = 1;
			
			StartCoroutine("InputDelay");
			
		}
		
		
		
		Spring.transform.position = new Vector3 ( transform.position.x, transform.position.y, transform.position.z) ;
		
		Patikelsystem.transform.position = new Vector3 ( transform.position.x, transform.position.y, transform.position.z) ;
		
		
		
		if(transform.position.y <= 2)
			
		{
			
			if(Input.GetKeyDown(KeyCode.Space)) {
				
				rigidbody.AddForce (0,300,0);
				
			}
			
		}
		
		
		
		if(transform.position.y <= -10 )
			
		{
			
			transform.position = startposition;
			
			rigidbody.angularVelocity = Vector3.zero;
			
			rigidbody.velocity = Vector3.zero;

			GUIShow.text = guiCount++;
			
			return;
			
		} 
		
		
		
		if(Input.GetKey(KeyCode.W ) ) {
			
			rigidbody.AddTorque(hallo1,0,0);
			
		} 
		
		
		
		if(Input.GetKey(KeyCode.S)) {
			
			rigidbody.AddTorque(-hallo,0,0);
			
		} 
		
		
		
		if(Input.GetKey(KeyCode.A)) {
			
			rigidbody.AddTorque(0,0,hallo);
			
		} 
		
		
		
		if(Input.GetKey(KeyCode.D)) {
			
			rigidbody.AddTorque(0,0,-hallo);
			
		}
		
	}
	
	
	
	float delay = 5.00f;
	
	IEnumerator InputDelay() {
		
		yield return new WaitForSeconds(delay);
		
		halloToggled = false;
		
		hallo = 10;
		hallo1 = 10;
		
	}
	
}

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 __Guis__how not public Gameobject __GUIS__how maybe you have overlooked that.

You’ll want to move the declaration of guiCount out of Update, and it will need to be in C# syntax.

int guiCount = 0;

Also you’ll need to change GUIShow.text to Guishow.text, and update the type of it to GUIText.