Syntax Errors (CS1513 and CS1022) Without Bracket Changes

Hi there!
I’m new to unity and trying to program a simple 2D game to get myself engaged. I decided to change a float variable to update with the timer, so i added more code and threw it into the void update section of my code.

I’m not entirely certain that the code I changed works, but I wouldn’t know because suddenly I’m getting a " } expected" error on line 27 and an “end-of-file” expected error on line 40. Is there something screwy in the line of code I changed (line 28) that would cause this? It was throwing an error in its original spot (between line 8 and 9) about “method name expected,” which I figured was occurring because it was now in the wrong place.

Sorry if anything I said above is incoherent, I have many years of experience with scratch but nothing else, so the finer aspects of hand-written code are still lost on me. Thanks in advance!

Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DeployPanel : MonoBehaviour
{
    public GameObject panelPrefab;
    private Vector2 screenBounds;
    public float xpos;
    // Start is called before the first frame update
    void Start()
    {
        screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
        StartCoroutine(panelWave());
    }

    private void spawnPanel()
    {
        GameObject a = Instantiate(panelPrefab) as GameObject;
        a.transform.position = new Vector2(Random.Range((-2f * screenBounds.x), (-0.2f * screenBounds.x)), screenBounds.y * -1);
        xpos = a.transform.position.x;
        GameObject b = Instantiate(panelPrefab) as GameObject;
        b.transform.position = new Vector2((xpos + 26), screenBounds.y * -1);
    }

    void Update()
    {
        public float respawnTime = (1.0f * (1/(1+((0.05f)(GameObject.Find("Timer").GetComponent<Timer>().tick)))));
    }
   
    IEnumerator panelWave()
    {
        while(true)
        {
            yield return new WaitForSeconds(respawnTime);
            spawnPanel();
        }
       
    }
}

Sure is.

  1. You have declared a public variable inside a method. You cannot do that. You have to declare your public variable outside your methods, just like you have with your other public variables at the top of you class.
  2. You have a lot of messy brackets going on, and I think you are meant to have something between (0.05f) and (GameObject.Find("Timer").GetComponent<Timer>().tick) because at the moment it you just have two sets of values between the brackets, but nothing for them to do with each other. Maybe you were meant to put * between them.

Adding to BaBiA’s good info above,

Remember the first rule of GameObject.Find():

Do not use GameObject.Find();

(ESPECIALLY not in Update()!!!)

More information: https://starmanta.gitbooks.io/unitytipsredux/content/first-question.html

1 Like