Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by El Maxo · Mar 28, 2014 at 10:25 AM · javascriptguiprefab

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;
 }
Comment
Add comment · Show 16
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image El Maxo · Mar 28, 2014 at 11:34 AM 0
Share

Sorry if you dont understand my English, just ask and ill try again.

avatar image Destran · Mar 28, 2014 at 11:47 AM 0
Share

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.

avatar image El Maxo · Mar 28, 2014 at 11:52 AM 0
Share

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.

avatar image Destran · Mar 28, 2014 at 12:02 PM 0
Share

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

avatar image El Maxo · Mar 28, 2014 at 12:13 PM 0
Share

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)

Show more comments

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges