I am working on the character selection screen for my game. I want it so that, when the player clicks on the select button, the code checks if the imagecollection in the charactermanger is 0, then loads the first character and so on.
Here are my three scripts:
Character Change
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterChange : MonoBehaviour
{
public static CharacterChange instance;
public static GameObject player1dub;
public GameObject Player;
public GameObject orangeShown;
public GameObject grapesShown;
public GameObject appleShown;
public GameObject bananaShown;
public Transform playerTransform;
public Transform orangeTransform;
public Transform grapesTransform;
public Transform appleTransform;
public Transform bananaTransform;
public Transform orangeHitPoint;
public Transform grapesHitPoint;
public Transform remyHitPoint;
public Transform appleHitPoint;
public Transform bananaHitPoint;
public Transform secondplayerhit;
public Transform orangeCameraMovement;
public Transform grapesCameraMovement;
public Transform remyCameraMovement;
public Transform appleCameraMovement;
public Transform bananaCameraMovement;
public Transform secondplayercamera;
PlayerMotion playerMotionScript;
CameraManager cameraManagerScript;
Gun gunScript;
Animator myAnimator;
Timer timerscript;
ScalingOfModels scalingScript;
AiSensor sensorScript;
public bool scaleBool;
public bool remyScaleBool;
public Vector3 endScale;
public Vector3 startScale;
public float timeDuration;
public float elapsedTime;
void Start()
{
playerMotionScript = GetComponentInParent<PlayerMotion>();
cameraManagerScript = FindObjectOfType<CameraManager>();
gunScript = FindObjectOfType<Gun>();
scalingScript = GetComponent<ScalingOfModels>();
myAnimator = GetComponent<Animator>();
timerscript = FindObjectOfType<Timer>();
sensorScript = GetComponent<AiSensor>();
startScale = playerMotionScript.selectedPlayer.transform.localScale;
}
public IEnumerator RemyChangeEnumerator()
{
if (playerMotionScript.selectedPlayer != Player)
{
playerMotionScript.selectedPlayer.SetActive(false);
elapsedTime += Time.deltaTime;
float complete = elapsedTime / timeDuration;
playerMotionScript.selectedPlayer.transform.localScale = Vector3.Lerp(endScale, startScale, complete);
playerTransform.position = playerMotionScript.selectedPlayer.transform.position;
Player.SetActive(true);
playerMotionScript.allHitPoints = remyHitPoint;
playerMotionScript.selectedPlayer = Player;
playerMotionScript.HandlePlayerChange();
cameraManagerScript.SetCameraMovement(remyCameraMovement);
yield return null;
}
}
private void OnTriggerEnter(Collider other)
{
Destroy(other.gameObject);
if (other.gameObject.tag == "OrangeCollision" && playerMotionScript.selectedPlayer != orangeShown)
{
ChangeCharacter(orangeShown, orangeTransform, orangeHitPoint, orangeCameraMovement);
}
else if (other.gameObject.tag == "GrapesCollision" && playerMotionScript.selectedPlayer != grapesShown)
{
ChangeCharacter(grapesShown, grapesTransform, grapesHitPoint, grapesCameraMovement);
}
else if (other.gameObject.tag == "AppleCollision" && playerMotionScript.selectedPlayer != appleShown)
{
ChangeCharacter(appleShown, appleTransform, appleHitPoint, appleCameraMovement);
}
else if (other.gameObject.tag == "BananaCollision" && playerMotionScript.selectedPlayer != bananaShown)
{
ChangeCharacter(bananaShown, bananaTransform, bananaHitPoint, bananaCameraMovement);
}
}
private void ChangeCharacter(GameObject newCharacter, Transform newTransform, Transform newHitPoint, Transform newCameraMovement)
{
playerMotionScript.selectedPlayer.SetActive(false);
elapsedTime += Time.deltaTime;
float complete = elapsedTime / timeDuration;
playerMotionScript.selectedPlayer.transform.localScale = Vector3.Lerp(endScale, startScale, complete);
newTransform.position = playerMotionScript.selectedPlayer.transform.position;
newCharacter.SetActive(true);
playerMotionScript.allHitPoints = newHitPoint;
playerMotionScript.selectedPlayer = newCharacter;
playerMotionScript.HandlePlayerChange();
cameraManagerScript.SetCameraMovement(newCameraMovement);
timerscript.linewait = 5;
timerscript.timerBar.fillAmount = 1;
newCharacter.GetComponent<ScalingOfModels>().scale = true;
}
}
SelectButton
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SelectButton : MonoBehaviour
{
public Button selectButton;
public GameObject[] characterPrefabs;
public CharactersManager managerScript;
// Start is called before the first frame update
void Start()
{
}
//Update is called once per frame
void Update()
{
}
public void CharactersSelect()
{
if (managerScript.imageCollection[0])
{
Debug.Log("Player is " + CharacterChange.instance.Player);
}
}
}
CharacterManager
using UnityEngine;
using UnityEngine.UI;
public class CharactersManager : MonoBehaviour
{
public Sprite[] imageCollection;
public Image displayImage;
private int currentImageIndex = 0;
public SelectButton selectScript;
void Start()
{
if (imageCollection.Length > 0)
{
displayImage.sprite = imageCollection[currentImageIndex];
}
}
public void OnNextButtonClick()
{
if (imageCollection.Length > 1)
{
currentImageIndex = (currentImageIndex + 1) % imageCollection.Length;
displayImage.sprite = imageCollection[currentImageIndex];
}
}
}
So how can I make the character selection work?