Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by michael777 · Nov 03, 2011 at 03:34 AM · instantiateclonecolorpicker

Color Picker / Instantiated Clones

How would I change the color of the Object thats about to get instantiated, instantiate a few cubes, then pick another color and not affect the ones that are already instantiated and have the new color only affect the next blocks or cubes that im going to instantiate?

Im instantiating a block (cube) several times:

 var cube = Instantiate(block, finalTemp, Quaternion.identity);
     cube.name = "Cube";


Im using this color picker:

   var xxx : float = 10;
     var yyy : float = 10;
     var colorPicker : Texture2D;
     
     // assign your model to this variable//commented out this was for testing the picker
     //var model : Renderer;
     
     // The material index of the material you want to pick a color for.
     var matnum : int = 0;
     
     //Color Picker
     function OnGUI() 
     {    
         if (GUI.RepeatButton (Rect (xxx,yyy,281,135), colorPicker)) 
         {
             var pickpos : Vector2 = Event.current.mousePosition;
             var pixelPosX : int = pickpos.x-xxx-9;//9
             var pixelPosY : int = 41-(pickpos.y-yyy-5);//5
             var col : Color = colorPicker.GetPixel(pixelPosX,pixelPosY);
             //model.materials[matnum].SetColor("_Color",col);
         }
     }



I've tried so many different variations but cant figure out the correct syntax... This was suggested as well:

 cube.renderer.material.color = new Color(1.0f, 0.0f, 0.0f);

but figuring out the right syntax has been a tough one I tried:

 var sm = GameObject.Find("Cube");
 sm.renderer.material.color = col;

but only the first cube thats instantiated will change color its not working quite correctly. Any suggestions or help would be GREATLY APPRECIATED!!!!

ok getting back to it.

Comment
Add comment · Show 3
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 syclamoth · Nov 03, 2011 at 03:36 AM 0
Share

[code] [/code] tags don't work on UA. I've updated your post with proper formatting, but learn how to do it correctly if you're going to post code here. The preview below your post is very helpful for this!

Basically you select all your code once it's pasted in, and press the '101010' button.

avatar image michael777 · Nov 03, 2011 at 03:40 AM 0
Share

I've just updated it :) any chance you can help with he question?

avatar image syclamoth · Nov 03, 2011 at 03:41 AM 0
Share

Working on it now! :)

5 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by syclamoth · Nov 03, 2011 at 03:40 AM

The way I would do this, would be to at the beginning of the scene instantiate a 'template' block which is either on an invisible layer, or otherwise outside of the player's view.

Then I would use the colour picker to change the material of that template, and instantiate it instead of the prefab! This way, you can change the colour of new blocks, but 1: you don't change the colour of old ones, and 2: you don't change the colour of the prefab either.

You could even put the 'template' in the corner of the screen as a 'preview' of what the blocks are going to look like!

Comment
Add comment · Show 1 · Share
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 syclamoth · Nov 03, 2011 at 03:47 AM 0
Share

Another thing- using GameObject.Find(string) is a pretty flaky way of finding things. Try to keep track of your objects in an array, or a list if you want to access them later. (using my method, you don't really need to do this, btw)

avatar image
0

Answer by michael777 · Nov 03, 2011 at 03:49 AM

Wow syclamoth, I would have never thought of something like that :) I trying it now and I seriously appreciate the advice brb.

Comment
Add comment · Share
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
0

Answer by michael777 · Nov 03, 2011 at 04:31 AM

I tried it but it still seems to be changing the color of all of the blocks.

I put a cube from the hierarchy into the model var slot then did

 var cube = Instantiate(model, finalTemp, Quaternion.identity);

and added new cubes which had the correct color but when I chose a new color for the next blocks they all changed to the new color I was now choosing.

I also tried instantiating a cube in the start function then instantiating the new blocks from the temp that was instantiated and I got the same results...

phew... I didn't think it would be this tough

any other ideas? I'll take any :)

Comment
Add comment · Show 2 · Share
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 syclamoth · Nov 03, 2011 at 04:41 AM 0
Share

$$anonymous$$ake sure you're modifiying 'material', not 'shared$$anonymous$$aterial'.

Wait, is the colour picker on each individual cube, or just on the template cube? $$anonymous$$ake sure that 1: there's only one colour picker, 2: it's definitely modifying the template, and nothing else, and 3: that the instantiated cubes aren't being modified by anything.

avatar image syclamoth · Nov 03, 2011 at 04:42 AM 0
Share

Instantiating the template in Start is the correct thing to do, btw.

avatar image
0

Answer by michael777 · Nov 04, 2011 at 12:46 AM

Hey syclamouth,

I seriously appreciate the help and effort in trying to figure this out :)

exiguous from the forums actually solved it and hats off to him ;) Here's the solution just incase anyone else needs it.

 var blockPrefab : Transform;//block prefab
 
 var xxx : float = 10;// position on x axis
 var yyy : float = 10;// position on y axis
 var colorPicker : Texture2D;// assign your colorpicker texture to this variable
 
 var cubecol : Color;  // change this with your colorpicker
 var cubecnt : int = 0;   // increase this when you instantiate a cube
 
 //instantiate code here
 if (Input.GetMouseButtonDown(0)) 
 {
        var cube = Instantiate(blockPrefab, finalTemp, Quaternion.identity);
                 cube.name = "Cube1_"+cubecnt.ToString();
                 cubecnt++;
                 cube.renderer.material.color = cubecol;
 }
 
 function OnGUI() 
 {    
     if (GUI.RepeatButton (Rect (xxx,yyy,281,135), colorPicker)) 
     {
         var pickpos : Vector2 = Event.current.mousePosition;
         var pixelPosX : int = pickpos.x-xxx-9;//9
         var pixelPosY : int = 41-(pickpos.y-yyy-5);//5
         var col : Color = colorPicker.GetPixel(pixelPosX,pixelPosY);
                 
         cubecol = col;
     }
 }

Amazing!

I love UnityAnswers & Unity Forums absolutely amazing people, thanks guys you make it GREAT!

Comment
Add comment · Share
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
0

Answer by tassawnt · Dec 12, 2020 at 11:42 AM

you can change the color by spriteRenderer color; just create a list of gameoject ,after you instantiate a gameboject add it to a list , then you can change the color of all clones of object by using foreach ,if you don't understant this just say it , and i will explain it more . @michael777

Comment
Add comment · Share
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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Instantiate Reference Problem 1 Answer

Cloned interactivecloth not acting like original interactivecloth. 0 Answers

Cloning Objects with Instantiate() - variables/references for added Components not stored? 3 Answers

How to Parent a Cloned Object to Another Cloned Object 1 Answer

Instantiate Clones Itself - Rather than Prefab 0 Answers


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