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. Dismiss Notice

Assets\ShootProjectile.cs(25,25): error CS0165: Use of unassigned local variable 'clone'

Discussion in 'Scripting' started by Jeremixx, Jan 5, 2021.

  1. Jeremixx

    Jeremixx

    Joined:
    Jan 3, 2021
    Posts:
    1
    Here is my code :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ShootProjectile : MonoBehaviour
    6. {
    7.     public GameObject ProjectileOBJ;
    8.  
    9.    
    10.     void Start()
    11.     {
    12.        
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         if(Input.GetKeyDown(KeyCode.F))
    19.              {
    20.                     GameObject cloneGOBJ;
    21.                         cloneGOBJ = GameObject.Find("ProjectileOBJ(Clone)");
    22.                           cloneGOBJ.SetActive(true);
    23.                     Rigidbody clone;
    24.                     cloneGOBJ = Instantiate(ProjectileOBJ);
    25.                         clone.AddForce(transform.forward);
    26.                 }
    27.        
    28.     }
    29. }
    Is there something wrong?
     
  2. payalzariya07

    payalzariya07

    Joined:
    Oct 5, 2018
    Posts:
    85
    In your scripts, You are not given the reference of Regibody (clone). Line no 23.
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Also line 22 will never work. GameObject.Find cannot find deactivated objects. Unclear what the point of lines 21 and 22 are anyway.