-3
\$\begingroup\$

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?

\$\endgroup\$
1
  • 2
    \$\begingroup\$ It's not clear to me what you want a reader to notice in these three scripts, or how the behaviour of these three scripts differs from what you want. Can you elaborate on what problem you need help solving, or what's not working? \$\endgroup\$ Commented Jul 14, 2024 at 12:20

1 Answer 1

0
\$\begingroup\$

As far as I understand you want to select first avaliable character of some array through static script that will save the picked unit. If that's the case, you can try the following script:

Script on the unit:

public class UnitScript
{
   [SerializeField] private bool available; // use this type of writting if you want easy access on the editor, and at the same time protection in the code
   public void SetAvailable(bool state) { available = state; }

   public bool GetAvailable() { return available; }

}

Your Code:

public static class CharacterChange
{
   public static GameObject PickedUnit;
   public static void SelectUnit(GameObject[] units) // call this function with array input
   { 
     
        foreach(GameObject unit in units) 
        {
            if (unit.GetComponent<UnitCode>().GetAvailable() == true)
            {  
                // you can make local variable and save the progress here, for example
                // PickedUnit = unit;
                // return; // return the wanted unit
            }
       }
   }
}

Attach the UnitScript to the units you wanna select/interact with.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.