- Home /
Question by
linelade · Apr 18, 2019 at 03:55 PM ·
scripting problem
changing between character controllers
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;
}
}
}
Comment
Best Answer
Answer by KevRev · Apr 18, 2019 at 04:21 PM
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;
}
}
}
$$anonymous$$y mobile changed case. $$anonymous$$ake sure the variable 'character' is referenced as such, and not 'Character'.
hey thanks a lot! yeah i figured the lower case out but now im stuck with another problem in line 27 it comes up with "Only assignment, call, increment, decrement, await and new object expressions can be used as statement"
its smth to do with the length but im blank on this :S
Yeah I get my ternary operators wrong lol.
Change it for this:
currentChar = currentChar < characters.Length ? currentChar++ : 0;
Your answer
