Home > Software engineering >  Assign A Random Integer To A Game Object For Referencing In Unity
Assign A Random Integer To A Game Object For Referencing In Unity

Time:02-23

Due to my inept ability to think, I've ran into a bit of a conundrum. When developing a card based system, I'm trying to assign each card an integer which can be used to stack the deck, instead of doing a random card per draw. Issue is, I have no clue how to assign a card a random number on every Start. I've come up with a...I would say...overcomplicated design, which I've left below. If anyone would be willing to point me in the right direction on how I would go about this, I would appreciate it. Apologies for this being a more...complex task, but I've searched everywhere for a solution, and cannot find it. Maybe I'm overthinking it. Who knows.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DeckOrder : MonoBehaviour
{
    //too many objects, maybe a way to simplify this process
    public GameObject card1;
    public GameObject card2;
    public GameObject card3;
    public GameObject card4;
    public GameObject card5;
    public GameObject card6;
    public GameObject card7;
    public GameObject card8;
    public GameObject card9;
    public GameObject card10;
    public GameObject card11;
    public GameObject card12;
    public GameObject card13;
    public GameObject card14;
    public GameObject card15;

    int randomNumber1;
    int randomNumber2;
    int randomNumber3;
    int randomNumber4;
    int randomNumber5;
    int randomNumber6;
    int randomNumber7;
    int randomNumber8;
    int randomNumber9;
    int randomNumber10;
    int randomNumber11;
    int randomNumber12;
    int randomNumber13;
    int randomNumber14;
    int randomNumber15;

    void Start()
    {
        //set card order
        randomNumber1 = Random.Range(1, 15);
            //beneath each would be a code telling it not to repeat that number it chose for the rest of these
        randomNumber2 = Random.Range(1, 15);
        randomNumber3 = Random.Range(1, 15);
        randomNumber4 = Random.Range(1, 15);
        randomNumber5 = Random.Range(1, 15);
        randomNumber6 = Random.Range(1, 15);
        randomNumber7 = Random.Range(1, 15);
        randomNumber8 = Random.Range(1, 15);
        randomNumber9 = Random.Range(1, 15);
        randomNumber10 = Random.Range(1, 15);
        randomNumber11 = Random.Range(1, 15);
        randomNumber12 = Random.Range(1, 15);
        randomNumber13 = Random.Range(1, 15);
        randomNumber14 = Random.Range(1, 15);
        randomNumber15 = Random.Range(1, 15);

        //this doesnt work, GameObject can't be converted to int or vise versa
        card1 = randomNumber1;
        card2 = randomNumber2;
        card3 = randomNumber3;
        card4 = randomNumber4;
        card5 = randomNumber5;
        card6 = randomNumber6;
        card7 = randomNumber7;
        card8 = randomNumber8;
        card9 = randomNumber9;
        card10 = randomNumber10;
        card11 = randomNumber11;
        card12 = randomNumber12;
        card13 = randomNumber13;
        card14 = randomNumber14;
        card15 = randomNumber15
    }
}

CodePudding user response:

In the beginning, everyone who entered this field was writing codes like you in the beginning and this is not a mistake, but the mistake lies in your staying at this low level

As for your question, it's not very clear but I will give you the basics so you can fix your code

If you have more than one variable with different values, you should use List Here you will find a toturial to explain how to use the list and where to use it

Here is a simple example of a List

[System.Serializable]
public struct Informaiton
{
    public string name;
    public int age;
    public int number;
}
public List<Informaiton> MyInformaiton = new List<Informaiton>();

public void Start()
{
    Informaiton _informaiton_ = new Informaiton();
    _informaiton_.name = "lelouch";
    _informaiton_.age = 17;
    _informaiton_.number = 1245836985;
    MyInformaiton.Add(_informaiton_);
}

I advise you to write this code and try to add things to it and remove things from it to adapt to the list more

Don't give up on the first try, nothing comes easy

CodePudding user response:

You should really use an array and then you could use Linq to simply shuffle the deck everytime like e.g.

using System.Linq;
using UnityEngine;

public class DeckOrder : MonoBehaviour
{
    public GameObject[] availableCards;

    public GameObject[] deck;

    private void Start ()
    {
        deck = availableCards.OrderBy(c => Random.value).ToArray();
    }
}

Now simply drag all your cards onto the name of the array availableCards -> they will all be added to the array in the Inspector.

Then on every start the deck will be filled in random order.

CodePudding user response:

The solution you're going with seems like a very brute force approach, I'd suggest trying to use an array to store all these numbers https://www.w3schools.com/cs/cs_arrays.php

Then you could write a function to create an array and assign random numbers.

  • Related