Posts by NIco

    i have adapted the updateLives from the course into my own game. The update lives only changes the display 1x.

    2 Pieces of code that I used are below. Please open my eyes!


    using System.Collections;

    using System.Collections.Generic;

    using UnityEngine;

    using UnityEngine.UI;


    public class UIManager : MonoBehaviour

    {

    public Sprite[] lives;

    public Image livesImageDisplay;



    public void UpdateLives(int currentLives)

    {

    Debug.Log("Player lives: " + currentLives);

    livesImageDisplay.sprite = lives[currentLives];

    }


    public void Updatescore()

    {


    }



    }


    and


    using System.Collections;

    using System.Collections.Generic;

    using UnityEngine;

    using UnityEngine.UI;


    public class killRedPiece : MonoBehaviour {




    public AudioClip clip;

    AudioSource audioSource;

    int redCount;


    GameObject[] tokensRed;

    [Header("Display Info")]

    public GameObject redWinsWindow;

    public GameObject greenWinsWindow;


    private AudioSource source;

    public AudioClip winSound;

    bool soundPlayed = false;


    private UIManager _uiManager;

    public int lives = 12;



    // Use this for initialization

    void Start()

    {

    _uiManager = GameObject.Find("Canvas").GetComponent<UIManager>();

    if (_uiManager != null)

    {

    _uiManager.UpdateLives(lives);

    }


    audioSource = GetComponent<AudioSource>();

    tokensRed = GameObject.FindGameObjectsWithTag("red");

    redCount = tokensRed.Length;


    }




    void OnMouseDown()

    {

    if (Input.GetMouseButtonDown(0))


    audioSource.PlayOneShot(clip, .5F);

    StartCoroutine(DestroyDelay());




    }


    IEnumerator DestroyDelay()

    {


    yield return new WaitForSeconds(1);

    lives--;

    _uiManager.UpdateLives(lives);

    Destroy(gameObject);

    }

    I have a sideboard and an inboard area in my game. 12 pieces start in the sideboard. The sideboard has to be empty before a player can move pieces on the board to new locations.

    I have managed to get all the pieces in the sideboard into a list and when I press a "ChangeTurns" button one of the pieces moves onto the board. What does not happen is that the list adds extra items rather than reduce. Here is my script so far:


    public class ObjectsInArea : MonoBehaviour

    {

    public GameObject[] redArray;//total number of red pieces in play


    public List<GameObject> sideBoardR;//total number of red pieces on the side

    public List<GameObject> inBoardR;//total number of red pieces that left the sideboard area



    void Start()

    {

    sideBoardR = new List<GameObject>();

    inBoardR = new List<GameObject>();


    redArray = GameObject.FindGameObjectsWithTag("red");


    for (int i = 0; i < redArray.Length; i++)

    {

    if (redArray[i].transform.position.x < -5.5f && redArray[i].transform.position.x > -6.4f && redArray[i].transform.localPosition.y < 2.7f && redArray[i].transform.localPosition.y > -2.3f)

    sideBoardR.Add(redArray[i]); // If in bounds add to correct list

    else

    inBoardR.Add(redArray[i]); // else add to the other list

    }

    // As jy wil kan jy die list in 'n array verander ook. Maar lists werk beter in die geval.

    // 'n List kan groei en krimp sonder om 'n size te spesifiseer in die begin

    //GameObject[] newArray = correctList.ToArray();


    }

    private void Update()

    {

    Debug.Log(sideBoardR.Count);

    }

    }


    AND THEN the part that does not work:


    public void MoveEnemy()

    {

    GameObject[] places; //there are 24 gameobj's - invisible - acting as slots

    Vector3 redTarget; //Enemy to move to

    redArray = GameObject.FindGameObjectsWithTag("red");

    places = GameObject.FindGameObjectsWithTag("place"); //list of slots objects



    for (int i = 0; i < redArray.Length; i++)

    {

    if (redArray[i].transform.position.x < -3.5f && redArray[i].transform.position.x > -10.4f && redArray[i].transform.localPosition.y < 1.5f && redArray[i].transform.localPosition.y > -2.5f)

    {

    sideBoardR.Add(redArray[i]); // If in bounds add to correct list

    }

    else

    inBoardR.Add(redArray[i]); // else add to the other list

    }


    int redNumber = Random.Range(0, sideBoardR.Count); //pick a random token and put in a var

    int placeLocation = Random.Range(0, places.Length); //pick a random item and put in a var



    if (sideBoardR.Count > 0)

    {

    redTarget = (places[placeLocation].transform.position);//put random location into temp var

    sideBoardR[redNumber].transform.position = redTarget;//set chosen piece pos = chosen slot pos

    sideBoardR.Remove(this.gameObject);


    Debug.Log("There are " + sideBoardR.Count + " red chips in the sideboard");


    }


    else if (sideBoardR.Count == 0)


    {

    return;

    }


    redTurn.SetActive(false);

    greenTurn.SetActive(true);


    }

    Am trying to count objects inside a rectangle I'm not using UI for it. These are pieces of a board game not yet placed on the board's playing area. Any help/hint appreciated

    This is a game where the player plays against the PC. The game is similar to "Mills" also called "12 man Morris" Each player starts with 12 pieces and has to place these on the boards taking turns. Then they start moving pieces around trying to get 3 pieces in a row, which gives that player the right to remove an enemy piece. Win condition is when a player only has 2 pieces remaining. So the number of pieces will get less. At the moment the enemy chooses a random piece and moves it whether its on the board or outside.:)

    This is what I'm trying to do:


    public GameObject[] tokens; //there are 12 green pieces

    GameObject[] places; //there are 24 gameobj's - invisible - acting as slots


    private GameObject chosenGPiece; //var for the chosen green piece


    void Start()


    {

    //make list green tokens(array)

    //choose a random item from the list/array

    //make a list of empty slots(array)

    //choose a random slot from the array

    //move the selected object to the selected slot



    tokens = GameObject.FindGameObjectsWithTag("green"); //list of green objects

    int indexNumber = Random.Range(0, tokens.Length); //get a random item and put in a var


    Debug.Log((tokens[indexNumber].name)); //just checking


    //chosenGPiece = GameObject.Find(indexNumber);

    //indexNumber.transform.position = new Vector3(0, 0, 0);


    places = GameObject.FindGameObjectsWithTag("place");

    int chosenSlot = Random.Range(0, places.Length);

    //Debug.Log((places[chosenSlot].GameObject));


    }