Quantcast
Viewing latest article 13
Browse Latest Browse All 131

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().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.

Viewing latest article 13
Browse Latest Browse All 131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>