Hidden object game randomization

Hello,
I am new to unity and trying to learn how to create a hidden object game. Right now I have the game object in the scene clickable and they match the text. I want to randomize their position in the scene based on predefined location. SO basically, I need to have a loockup table with all the possible position and when the game start I need to randomly position each item based on the the lookup table then discard this position. I also need to check if the color of the item would fit with the picked position or not. I am a bit lost how to do it so if anyone can point me to a certain tutorial or code I would highly appreciate it.

Thank you

Walaa

Steps to success:

Create the scene with the background setup

Create a script with a GameObject array variable:

public GameObject[] PossibleLocations;

Drag a single instance of that script into the scene (on a new empty GameObject)

That will be where all the possible locations get noted.

FOR EACH POSSIBLE LOCATION:

—> Move your hidden object graphic where you want it, rotate it how you want, etc.

—> Make a child empty GameObject under the hidden object

—> Deparent the empty GameObject (it will retain position and rotation)

—> Drag the empty GameObject into the PossibleLocations array

—> Do that until you have created invisible GameObjects exactly where you want the hidden object to be.

Now when you emplace the object:

  • choose a random index, 0 through PossibleLocations.Length - 1

  • Spawn the hidden object as a child to that empty GameObject

  • put an appropriate collider on that hidden object and test for the user clicking it.

WIN!

thank you so much kurt, so I will have to create multiple instance of each object in different location. I was hoping to load object on runtime and randomize there position. My objects are not fixed they are text based and should get populated from on runtime if I am clear???

Another way to go is to drag the hidden object into the scene in ALL the places you want it, all simultaneously, then at start, destroy all but one of them in Start().

Might even be simpler, now that I think of it… You could put them all as children of a single object and then just access that object and consider its children as the available pieces to choose from. Only one object to drag into the script, then it will work forever into the future, even if you remove / add more hidden object instances.

I trying to do it using the script below , what happens is that some items get instantiated in the same position and overlap with each other.

7620139--947494--upload_2021-11-1_12-7-4.png

Also when I created the prefab I had a text attached to the script it is missing and I don’t know how to make it show again. the picture might explain better. this object name text is a text mesh pro attached to a slot in a inventory

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

public class RandomSpawner : MonoBehaviour
{
    // Start is called before the first frame update

    public GameObject[] Position  = new GameObject[4];    // Number of placements on the screen
    public GameObject[] BinaryItem;        // The binary number  , that have to be placed randomly on the screen

    void Start()
    {
        BinarySpawner();    // Call the binary spawner Function
    }

    void BinarySpawner()
    {
        for (int i = 0; i < BinaryItem.Length; i++)    // Loop through all slots on the Screen
        {
            //int randomItem = Random.Range(0, i);        // Get Random binary number
            int j = Random.Range(0, Position.Length);
            Instantiate(BinaryItem[i], Position[j].transform.position, Position[j].transform.rotation);
        }

    }

   
}

can you please help me I am stuck

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong: