in my C# program there is an error which I can`t fix. @username
C# program there is an error it says the private field Blockawn.block
is assigned but is never used. The C# script is below.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BlockSpawn : MonoBehaviour
{
public GameObject[] Blocks;
public Transform BlockSpawnPos;
public int BlockCount = 0;
public float NewPos;
private int randomNum;
private float waitTime = 2.0f;
private GameObject block;
// Use this for initialization
void Start ()
{
Block();
}
// Update is called once per frame
void Update ()
{
}
public void Block()
{
block = Instantiate (Blocks [Random.Range (0, 5)], BlockSpawnPos.position, Quaternion.identity) as GameObject;
Vector3 temp = BlockSpawnPos.position;
temp.y = 0;
temp.x += 5;
temp.z = 0;
BlockSpawnPos.position = temp;
StartCoroutine (wait());
}
IEnumerator wait(){
yield return new WaitForSeconds (waitTime);
BlockCount += 1;
Block ();
}
}
Answer by Commoble · Apr 14, 2017 at 03:46 PM
That's not an error, it's a warning. Since private fields can only be referred to in the same script they're declared in, then if that script doesn't use it, it's not being used anywhere. Your IDE is letting you know that you've declared a variable and you aren't using it.
When you use Instantiate to create an object, the object will exist in the scene even if you don't assign the return value to anything. If you don't plan on referring to elsewhere in that script, you can safely remove private GameObject block;
from the script.
Alternatively, if you're going to add in something that uses the reference to the block object later, you can just ignore the warning.
Your answer
Follow this Question
Related Questions
Failed extracting collision mesh error 0 Answers
Scripting problem 1 Answer
Required API level 26, android sdk 2 Answers
InvalidCastException: Cannot cast from source type to destination type 0 Answers