Greetings Fellow Developers,
I'm constructing a Game for a Project which requires me to spawn in random blocks (or Prefabs to use technical terms) on the end of the map so they spawn a random Block each time, which in effect makes an endless map.
It all worked fine until I followed the tutorial section which Randomises the Blocks (prefabs being Spawned). The Compiler Error says: not all code paths return a value.
The code is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BlockManager : MonoBehaviour
{
//Creating a GameObject to drag-and-drop BlockPrefabs into for use.
public GameObject[] blockPrefabs;
//declaring variables for use in the Script
private Transform playerTransform;
//distance behind the character where blocks are spawned
private float spawnZAxis = -10.0f;
//The length of the blocks being used in the Script/Game
private float blockLength = 50.0f;
//default amount of blocks on the screen
private int amnBlocks = 4;
//Records the first block used so a blank one will never be spawned in again
private int lastBlockUsed = 0;
//This section finds the Player Object and acquires it's position, from there, it then spawns a block for the Game Character.
void Start()
{
//Assigning the List to the Variable
//activeBlocks = new List();
playerTransform = GameObject.FindGameObjectWithTag("PlayerObject").transform;
//Here shall detect when the Player moves a certain distance and then calls the functions to make a Block and Delete the End one.
for (int i = 0; i < amnBlocks; i++)
{
SpawnBlock();
//DeleteBlock();
}
}
// Update is called once per frame. Thos will call the SpawnBlock Function when the player moves along a certain distance on the game.
void Update()
{
if (playerTransform.position.z > (spawnZAxis - amnBlocks * blockLength))
{
SpawnBlock();
}
}
private void SpawnBlock(int prefabIndex = -1)
{
GameObject go;
go = Instantiate(blockPrefabs [RandomBlock()]) as GameObject;
go.transform.SetParent(transform);
go.transform.position = Vector3.forward * spawnZAxis;
spawnZAxis += blockLength;
}
private int RandomBlock()
{
if (blockPrefabs.Length <= 1)
return 0;
int randomInt = lastBlockUsed;
while(randomInt == lastBlockUsed)
{
randomInt = Random.Range(0, blockPrefabs.Length);
}
}
}
The Issue is the private int RandomBlock() Section towards the bottom which it doesn't like?
I'm stumped. I'm an absolute noob at Unity so a solution to this would be Gucci!
Thanks in advance Fellow Developers,
C
↧