- Home /
I'm struggling with assigning unique attributes to prefab objects.
As a test for a much larger project, I'm creating five primitive cubes (prefabs) and want to assign each a unique "Name" and two unique numbers assigned at random...I plan to access and manipulate these from other scripts. I have no difficulty creating unique names and accessing them from other scripts. However, I'm stumped on how to assign the numbers. This is what I currently have although I've tried all sorts of things and have looked at many, many previous posts but I can't seem to find one on point.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestScript : MonoBehaviour
{
public GameObject testCube;
private int numCubes = 5;
private int[] testArray = new int[2];
void Start()
{
for (int i = 0; i < numCubes; i++)
{
Vector3 position = new Vector3(Random.Range(5f, 45f), .5f, Random.Range(5f, 45f));
GameObject cloneCube = Instantiate(testCube, position, Quaternion.identity);
cloneCube.gameObject.GetComponent<Renderer>().material.color = Color.green;
string stringA = "Name: ";
string stringB = i.ToString();
cloneCube.name = stringA + stringB + stringB + stringB;
testArray[0] = Random.Range(20, 30);
testArray[1] = Random.Range(20, 30);
}
}
}
I've been working on this for quite some time now. I've got the name to work but I'm still having trouble with the numbers.
Note: So far, I've had no difficulty getting the prefabs to appear on a small test terrain. That part works fine (I have five nice pretty green cubes). The problem is just in assigning each prefab a couple of unique numbers.
Where do you want these numbers? In a script or the name?
Thanks for responding. I'm not certain if I understand your question. I want to assign certain unique attributes to each prefab so I can access them (and change them) later from other scripts. $$anonymous$$y problem is that I'm not sure how to create these unique attributes for each prefab. To be honest, I don't care how or where they are created so long as I can access and change them. For example, if I "click" on one of my objects, I want to be able to display the numbers in a UI (I can create the UI and I can get each unique name to appear just fine … I'm just struggling with the numbers, etc.). Thanks for your help.,
Answer by Topthink · Mar 02, 2019 at 07:49 PM
Okay, I have figured out how to get the unique numbers I need. Now I just need to figure how to access them from my other scripts. Will work on this.
Answer by sath · Mar 02, 2019 at 07:53 PM
In order to access testArray from other scripts it must be public and not private. Your script is working fine. Set the array as public and you will see in the inspector that each pretty green cube has generated an array of 2 random numbers. Be aware although that random selection does not guarantee that every cube will have an array with unique values. If you want to accomplish that you must have a public list of numbers in an other script and set every cube script to remove the numbers that have been selected from the list.
Thank you kind sir. By trial and error, I figured out how to create the numbers for each prefab (I should have said "random" ins$$anonymous$$d of "unique"). I did change that to "public" -- I tried all sorts of stuff there. I also figured out how to access the information from my other scripts. Thank you for taking the time to help me.
I'm just a one person operation and it all took me several hours before I got it to work as desired. It's funny how it often seems that the solution is "obvious" after the problem is resolved. If I could only see these things before I'd almost be a programmer. ;)
Answer by Symposium10000 · Mar 11, 2019 at 02:53 PM
topthink - not sure if i'm on the right thread (i'm a t work and only have a minute), but someone solved my textmesh problem and it works: http://answers.unity.com/comments/1611286/view.html
good luck!