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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

The name "i" does not exist in current context issue

Discussion in 'Scripting' started by walker123t, May 23, 2018.

  1. walker123t

    walker123t

    Joined:
    May 23, 2018
    Posts:
    1
    I'm just getting started in C# and I'm unable to get this code to work. Am I doing something wrong?
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class BlockSpawner : MonoBehaviour
    4. {
    5.  
    6.     public Transform[] spawnPoints;
    7.  
    8.     public GameObject blockPrefab;
    9.  
    10.     public float timeBetweenWaves = 1f;
    11.  
    12.     private float timeToSpawn = 2f;
    13.  
    14.     void Update()
    15.     {
    16.  
    17.         if (Time.time >= timeToSpawn)
    18.         {
    19.             SpawnBlocks();
    20.             timeToSpawn = Time.time + timeBetweenWaves;
    21.         }
    22.  
    23.     }
    24.  
    25.     void SpawnBlocks()
    26.     {
    27.         int randomIndex = Random.Range(0, spawnPoints.Length);
    28.  
    29.         for (int i = 0; i < spawnPoints.Length; i++)
    30.         {
    31.             if (randomIndex != i)
    32.             {
    33.                 Instantiate(blockPrefab, spawnPoints[i].position, Quaternion.identity);
    34.             }
    35.         }
    36.     }
    37. }
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,383
    No errors in that script. Are you sure you posted the right one?
     
  3. Prastiwar

    Prastiwar

    Joined:
    Jul 29, 2017
    Posts:
    125
    If you got error, select them, tell us which line is it or comment this line. However I don't see any error in this code.
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    In replying to the thread headline - I wonder, did you maybe have a semi colon at the end of line 29 meaning that "i" went out of scope for the subsequent lines?

    Presumably the expectation is that this creates an object at all bar one spawn points at each periodic interval. What actually happened instead to make you think it is not working?