hey guys
i want to switch between 3 FPS in my game.
this is the code I have so far but somehow its not working what am I doing wrong?
using System.Collections;
using System.Collections.Generic;
using UnityStandardAssets.Characters.FirstPerson;
using UnityEngine;
public class World : MonoBehaviour {
public GameObject [] characters;
GameObject currentCharacter;
int charactersIndex;
void Start () {
charactersIndex = 0;
currentCharacter = characters [0];
}
void Update () {
if(Input.GetButtonDown("Fire1")){
charactersIndex++;
if (charactersIndex == characters.Length) {
charactersIndex = 0;
}
currentCharacter.GetComponent<FirstPersonController> ().enabled = false;
characters[charactersIndex].GetComponent<FirstPersonController> ().enabled = true;
}
}
}
KevRev
2
Try this:
using System.Collections;
using System.Collections.Generic;
using UnityStandardAssets.Characters.FirstPerson;
using UnityEngine;
public class World : MonoBehaviour
{
public List<GameObject> characters = new List<GameObject>();
int currentChar = 0;
void Start()
{
// Disable all first
foreach (GameObject character in characters)
{
Character.GetComponent<FirstPersonController>().enabled = false;
}
// Enable only the one you want
characters[currentChar].GetComponent<FirstPersonController>().enabled = true;
}
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
currentChar < characters.Length ? currentChar++ : currentChar = 0;
// Disable all first
foreach (GameObject character in characters)
{
Character.GetComponent<FirstPersonController>().enabled = false;
}
// Enable only the one you want
characters[currentChar].GetComponent<FirstPersonController>().enabled = true;
}
}
}