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

The name 'lspikeclone' does not exist in the current context

Discussion in 'Scripting' started by MuduXD, Nov 7, 2019.

  1. MuduXD

    MuduXD

    Joined:
    Oct 20, 2019
    Posts:
    3
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using UnityEngine.SceneManagement;
    7.  
    8. public class PlayerMovement : MonoBehaviour
    9. {
    10.     public Rigidbody2D rb;
    11.     public GameObject MainMenuUI;
    12.     public GameObject DeathMenuUI;
    13.     public GameObject LeftSpikePrefab;
    14.     public GameObject RightSpikePrefab;
    15.     Vector2 movement;
    16.     public int score = 0;
    17.     int[] leftspike = new int[10];
    18.     int i=1,c=0,moveSpeed=4;
    19.     void Start()
    20.     {
    21.         Time.timeScale = 0;
    22.         MainMenuUI.SetActive(true);
    23.         movement.x = 1;
    24.         movement.y = 0;
    25.     }
    26.  
    27.     void Update()
    28.     {
    29.         movement.y = movement.y - 2 * Time.deltaTime;
    30.          rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
    31.     }
    32.     //JUMPING
    33.     public void Jump()
    34.     {
    35.         Time.timeScale = 1;
    36.         MainMenuUI.SetActive(false);
    37.         movement.y = movement.y + 100 * Time.deltaTime;
    38.     }
    39.     //SPAWNING SPIKES
    40.     void SpawnRightSpike(int i)
    41.     {
    42.         for (; i >= 0; i--)
    43.         {
    44.             GameObject rspikeclone = Instantiate(RightSpikePrefab, new Vector2(2.7f, Random.Range(-4, 4)), Quaternion.identity) as GameObject;
    45.         }
    46.     }
    47.     void SpawnLeftSpike(int i)
    48.     {
    49.         for (; i >= 0; i--)
    50.         {
    51.             GameObject lspikeclone = Instantiate(LeftSpikePrefab, new Vector2(2.7f, Random.Range(-4, 4)), Quaternion.identity) as GameObject;
    52.         }
    53.     }
    54.     //DESTROYING SPIKES
    55.     void DestroyLeftSpike(int i)
    56.     {
    57.         for (; i >= 0; i--)
    58.             Destroy(lspikeclone);
    59.     }
    60.     void DestroyRightSpike(int i)
    61.     {
    62.         for (; i >= 0; i--)
    63.             Destroy(rspikeclone);
    64.     }
    65.     //COLLISSION DETECTION
    66.     void OnTriggerEnter2D(Collider2D collider)
    67.     {
    68.         if (collider.gameObject.tag == "Spike")
    69.         {
    70.             DeathMenuUI.SetActive(true);
    71.             Time.timeScale = 0;
    72.         }
    73.         if (collider.gameObject.tag == "LeftBorder")
    74.         {
    75.             movement.x = 1;
    76.             transform.localRotation = Quaternion.Euler(0, 360, 0);
    77.             score++;
    78.             SpawnRightSpike(i);
    79.             DestroyLeftSpike(i);
    80.             if (i < 7)
    81.             {
    82.                 if (c == i)
    83.                 {
    84.                     i++;
    85.                     c = 0;
    86.                 }
    87.                 else
    88.                 {
    89.                     c++;
    90.                 }
    91.             }
    92.         }
    93.         if (collider.gameObject.tag == "RightBorder")
    94.         {
    95.             movement.x = -1;
    96.             transform.localRotation = Quaternion.Euler(0, 180, 0);
    97.             score++;
    98.             SpawnLeftSpike(i);
    99.             DestroyRightSpike(i);
    100.             if (i < 7)
    101.             {
    102.                 if (c == i)
    103.                 {
    104.                     i++;
    105.                     c = 0;
    106.                 }
    107.                 else
    108.                 {
    109.                     c++;
    110.                 }
    111.             }
    112.         }
    113.     }
    114. }
     
  2. MuduXD

    MuduXD

    Joined:
    Oct 20, 2019
    Posts:
    3
    I'm a beginner and this is my first game that I'm trying to make but I don't know how to solve this error.I would be thankful if anyone could help me
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    You declare the local variable on line 51, and when you do that it's valid only inside that { } code block.

    Declare the variable (`GameObject lspikeclone;`) outside the function, and just do the assignment part in that loop. Then it will be accessible everywhere in the class.
     
  4. MuduXD

    MuduXD

    Joined:
    Oct 20, 2019
    Posts:
    3
    Thank you so much