- Home /
Assign "Colour" to Prefab
Ok I have this code below that randomly spawns in a enemy. What I need to do is after the random prefab is spawned, I need to find out its colour and display it on a GUI. My idea would be to make a new script and change it some how for each individual prefab. although my problem as well would be how to locate it. If anyone can suggest something, i would appreciate it very much.
#pragma strict
var enemyPrefabs : GameObject[]; // Array of different Enemies that are used.
var spawnPoints : Transform[]; // Array of spawn points to be used.
var Name1Choices : String[] = ["Bob", "Mary", "Maxo","Phil","Noobas","Hervard","Dan","Daniel","Johnathan"];
var Name2Choices : String[] = ["Berry", "Richards", "Stephens"];
var Backstory1Choices : String[] = ["Was"];
var Backstory2Choices : String[] = ["a"];
var Backstory3Choices : String[] = ["gent"];
var Name1;
var Name2;
var Backstory1;
var Backstory2;
var Backstory3;
var Nameone : GUIText;
var Nametwo : GUIText;
var Money : GUIText;
var mission : boolean = false;
var money : float;
function Start ()
{
MissionGen();
}
function MissionGen ()
{
while(true)
{
var pos : Transform = spawnPoints[Random.Range(0, spawnPoints.length)];
var obj : GameObject = enemyPrefabs[Random.Range(0, enemyPrefabs.length)];
Instantiate(obj, pos.position, pos.rotation);
Name1 = Name1Choices[ Random.Range( 0, Name1Choices.length ) ];
Name2 = Name2Choices[ Random.Range( 0, Name2Choices.length ) ];
Backstory1 = Backstory1Choices[ Random.Range( 0, Backstory1Choices.length ) ];
Backstory2 = Backstory2Choices[ Random.Range( 0, Backstory2Choices.length ) ];
Backstory3 = Backstory3Choices[ Random.Range( 0, Backstory3Choices.length ) ];
Nameone.text = Name1;
Nametwo.text = Name2;
mission = true;
Money.text = "Money: " + money.ToString();
yield WaitForSeconds (1);
while (!Check())
{
yield WaitForSeconds (1);
}
yield WaitForSeconds (1);
}
}
function Check()
{
var BadGuyCheck : GameObject = GameObject.FindGameObjectWithTag( "BadGuy" );
if ( BadGuyCheck == null )
{
mission = false;
money += Random.Range(1000,10000);
return true;
}
return false;
}
Sorry if you dont understand my English, just ask and ill try again.
Hmm you could do something like:
var testString : String = "";
function Start ()
{
testString = gameObject.renderer.material.color.ToString();
}
And then display that string on the gui. Only problem is that it will give you something like "RGBA(1.0,1.0,1.0,.5)" which I'm almost certain isn't what you want. I guess a weird and inconvenient way around this would to be do something like this:
var testString : String = "";
function Start ()
{
if(gameObject.renderer.material.color == Color.red)
{
print("color is red");
testString = "Red";
}
if(gameObject.renderer.material.color == Color.green)
{
print("color is red");
testString = "Green";
}
}
This would only work if you're using simple colors like "black,red,blue,yellow,cyan,green,grey, etc..." but it would work.
Thanks, I guess that could work, I was thinking that for example made a bit of code that I attach, this code would be like color= (then have the ablity to say what it is in unity). then going back to my main code have one that scans the scene for the prefab, and locates the color. I just have no idea how to do that.
Yea it's not exactly ideal but I can't think of any other way to tell what the color is. $$anonymous$$aybe there's some kind of function that I'm not aware of. When you spawn your prefabs, are you giving them different colors? Or do they all have the same color? Give me a little more info on your game and I'll try to think of something better. :D
Well there all ready got different colors on them, so there all pre made up. (if you get me). In the game you have to locate a certain AI and remove it. So i am getting information so the player can kill it. (sorry if im losing you)
Your answer
![](https://koobas.hobune.stream/wayback/20220613141404im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
Edit Prefab, Instantiate, Access Array in Clone 1 Answer
Prevent instantiating on collider 1 Answer
Gui Text Script 4 Answers
GUI Button Disappearing 1 Answer