Please Help "error CS1525: Unexpected symbol `void'''
I'm new to C# :) I cant speak wery well english because ı'm Turkish :P I can't start my game Unity3D says "All complier error have to be fixed" My codes:
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Karakter : MonoBehaviour {
public float hiz,maxHiz,ziplamaGücü;
public bool yerdemi,ciftZipla;
public int can, maxCan, havuc, altin;
public GameObject[] canlar;
public Text Altinmiktar, Havucmiktar;
public AudioClip[] sesler;
Rigidbody2D agirlik; Animator anim;
void Start () {
anim = GetComponent<Animator> ();
agirlik = GetComponent<Rigidbody2D> ();
can = maxCan;
canSistemi ();
}
void Update () {
Altinmiktar.text = " " + altin;
Havucmiktar.text = " " + havuc;
if (Input.GetKeyDown (KeyCode.R)) {
Application.LoadLevel (Application.loadedLevel);
}
//////////ZIPLAMA//////////
if (Input.GetKeyDown (KeyCode.UpArrow)) {
if (yerdemi) {
agirlik.AddForce (Vector2.up * ziplamaGücü);
ciftZipla = true;
} else {
if (ciftZipla) {
ciftZipla = false;
agirlik.AddForce (Vector2.up * ziplamaGücü);
}
}
}
if (Input.GetKeyDown (KeyCode.W)) {
if (yerdemi) {
agirlik.AddForce (Vector2.up * ziplamaGücü);
ciftZipla = true;
} else {
if (ciftZipla) {
ciftZipla = false;
agirlik.AddForce (Vector2.up * ziplamaGücü);
}
}
}
if (Input.GetKeyDown (KeyCode.Space)) {
if (yerdemi) {
agirlik.AddForce (Vector2.up * ziplamaGücü);
ciftZipla = true;
} else {
if (ciftZipla) {
ciftZipla = false;
agirlik.AddForce (Vector2.up * ziplamaGücü);
}
}
}
/////////ZIPLAMA///////////
if (can <= 0) {
olme ();
}
}
void FixedUpdate(){
float h = Input.GetAxis("Horizontal");
agirlik.AddForce (Vector3.right * h * hiz);
anim.SetFloat ("Hiz", Mathf.Abs (h));
anim.SetBool ("Yerde", yerdemi);
if (h > 0.1f) {
transform.localScale = new Vector2 (1, 1);
}
if (h < 0.1f) {
transform.localScale = new Vector2 (-1, 1);
}
if (agirlik.velocity.x > maxHiz) {
agirlik.velocity = new Vector2 (maxHiz, agirlik.velocity.y);
}
if (agirlik.velocity.x < -maxHiz) {
agirlik.velocity = new Vector2 (-maxHiz, agirlik.velocity.y);
}
}
void olme(){
Application.LoadLevel (Application.loadedLevel);
}
void OnCollisionEnter2D (Collision2D nesne){
if (nesne.gameObject.tag == "Tuzak") {
can -= 1;
agirlik.AddForce (Vector2.up * ziplamaGücü);
GetComponent<AudioSource> ().PlayOneShot (sesler[3]);
GetComponent<SpriteRenderer> ().color = Color.red;
Invoke ("Duzelt", 0.5f);
canSistemi ();
}
}
}
void canSistemi(){
for (int i = 0; i < maxCan; i++) {
canlar [i].SetActive (false);
}
for (int i = 0; i < can; i++) {
canlar [i].SetActive (true);
}
}
void Duzelt(){
GetComponent<SpriteRenderer> ().color = Color.white;
}
void OnTriggerEnter2D (Collider2D nesne) {
if (nesne.gameObject.tag == "Havuc") {
havuc++;
GetComponent<AudioSource> ().PlayOneShot (sesler[1]);
Destroy (nesne.gameObject);
}
if (nesne.gameObject.tag == "Altin") {
altin++;
GetComponent<AudioSource> ().PlayOneShot (sesler[1]);
Destroy (nesne.gameObject);
}
if (nesne.gameObject.tag == "Can") {
if (can != maxCan) {
can++;
canSistemi ();
GetComponent<SpriteRenderer> ().color = Color.green;
GetComponent<AudioSource> ().PlayOneShot (sesler[2]);
Invoke ("Duzelt",0.5f);
Destroy (nesne.gameObject);
}
}
}
}
}
And my error is: Assets/Kodlar/Karakter.cs(117,0): error CS1525: Unexpected symbol `void'
Please help mee :)
Answer by TBruce · Dec 11, 2016 at 07:38 PM
Hi @Canbey06,
I have fixed and tested your script and it no longer has errors (for me to test I had to change all occurrences of ziplamaGücü
to ziplamaGucu
as ü
is an unrecognized character on my system).
You also had some extra curly braces which I remove. I also created references for the following
gameObject.GetComponent<AudioSource> ()
gameObject.GetComponent<SpriteRenderer> ()
in the Start()
function because you were calling them frequently. Please forgive me for changing the formatting of your code as it helps me read easier. Below is your updated class
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Karakter : MonoBehaviour
{
public float hiz ,maxHiz, ziplamaGücü;
public bool yerdemi, ciftZipla;
public int can, maxCan, havuc, altin;
public GameObject[] canlar;
public Text Altinmiktar, Havucmiktar;
public AudioClip[] sesler;
Rigidbody2D agirlik; Animator anim;
AudioSource audioSource;
SpriteRenderer spriteRenderer;
void Start ()
{
anim = GetComponent<Animator> ();
agirlik = gameObject.GetComponent<Rigidbody2D> ();
audioSource = gameObject.GetComponent<AudioSource> ();
spriteRenderer = gameObject.GetComponent<SpriteRenderer> ();
can = maxCan;
canSistemi ();
}
void Update ()
{
Altinmiktar.text = " " + altin;
Havucmiktar.text = " " + havuc;
if (Input.GetKeyDown (KeyCode.R))
{
Application.LoadLevel (Application.loadedLevel);
}
//////////ZIPLAMA//////////
if (Input.GetKeyDown (KeyCode.UpArrow))
{
if (yerdemi)
{
agirlik.AddForce (Vector2.up * ziplamaGücü);
ciftZipla = true;
}
else
{
if (ciftZipla)
{
ciftZipla = false;
agirlik.AddForce (Vector2.up * ziplamaGücü);
}
}
}
if (Input.GetKeyDown (KeyCode.W))
{
if (yerdemi)
{
agirlik.AddForce (Vector2.up * ziplamaGücü);
ciftZipla = true;
}
else
{
if (ciftZipla)
{
ciftZipla = false;
agirlik.AddForce (Vector2.up * ziplamaGücü);
}
}
}
if (Input.GetKeyDown (KeyCode.Space))
{
if (yerdemi)
{
agirlik.AddForce (Vector2.up * ziplamaGücü);
ciftZipla = true;
}
else
{
if (ciftZipla)
{
ciftZipla = false;
agirlik.AddForce (Vector2.up * ziplamaGücü);
}
}
}
/////////ZIPLAMA///////////
if (can <= 0)
{
olme ();
}
}
void FixedUpdate()
{
float h = Input.GetAxis("Horizontal");
agirlik.AddForce (Vector3.right * h * hiz);
anim.SetFloat ("Hiz", Mathf.Abs (h));
anim.SetBool ("Yerde", yerdemi);
if (h > 0.1f)
{
transform.localScale = new Vector2 (1, 1);
}
if (h < 0.1f)
{
transform.localScale = new Vector2 (-1, 1);
}
if (agirlik.velocity.x > maxHiz)
{
agirlik.velocity = new Vector2 (maxHiz, agirlik.velocity.y);
}
if (agirlik.velocity.x < -maxHiz)
{
agirlik.velocity = new Vector2 (-maxHiz, agirlik.velocity.y);
}
}
void olme()
{
Application.LoadLevel (Application.loadedLevel);
}
void OnCollisionEnter2D (Collision2D nesne)
{
if (nesne.gameObject.tag == "Tuzak")
{
can -= 1;
agirlik.AddForce (Vector2.up * ziplamaGücü);
audioSource.PlayOneShot (sesler[3]);
spriteRenderer.color = Color.red;
Invoke ("Duzelt", 0.5f);
canSistemi ();
}
}
void canSistemi()
{
for (int i = 0; i < maxCan; i++)
{
canlar [i].SetActive (false);
}
for (int i = 0; i < can; i++)
{
canlar [i].SetActive (true);
}
}
void Duzelt()
{
spriteRenderer.color = Color.white;
}
void OnTriggerEnter2D (Collider2D nesne)
{
if (nesne.gameObject.tag == "Havuc")
{
havuc++;
audioSource.PlayOneShot (sesler[1]);
Destroy (nesne.gameObject);
}
if (nesne.gameObject.tag == "Altin")
{
altin++;
audioSource.PlayOneShot (sesler[1]);
Destroy (nesne.gameObject);
}
if (nesne.gameObject.tag == "Can")
{
if (can != maxCan)
{
can++;
canSistemi ();
spriteRenderer.color = Color.green;
audioSource.PlayOneShot (sesler[2]);
Invoke ("Duzelt",0.5f);
Destroy (nesne.gameObject);
}
}
}
}