Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Apr 17, 2018 at 10:05 PM by polan31 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by polan31 · Apr 12, 2018 at 09:51 AM · 2dcollisioncolor change

How to change the color of the ball after hitting square?

C# 2D

I have a multi colored square ( PIC I).

My BALL (PIC II) hits him.

How to change the color of the ball after hitting square?

The point is to make the ball change its color to the color it strikes.

E.g:

The ball hits the red part of square- ball turns red

The ball hits the green part of square - bal changes color to green

etc. etc.

I know I should use a collision, but I don't know how. alt text alt text

rys-1.png (5.9 kB)
rys-2.png (5.3 kB)
Comment
Add comment
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

5 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by kskjadav007 · Apr 13, 2018 at 09:03 AM

i tried this and worked

     void OnCollisionEnter2D(Collision2D coll) 
     {
         if(coll.gameObject.tag=="Enemy")
         {
             Debug.Log ("Collision");
 
 
             Color one= coll.gameObject.GetComponent <SpriteRenderer>().color;
 
             gameObject.GetComponent <SpriteRenderer>().color=one;
         }
     }


here is demo

link text

Comment
Add comment · Show 9 · 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 polan31 · Apr 13, 2018 at 09:52 AM 0
Share

Hi.

Could you possibly show me screen of the ball and square in the Inspector?

I would be grateful if I saw how it looks there with the materials.

avatar image kskjadav007 polan31 · Apr 13, 2018 at 10:42 AM 0
Share

it was sprites-default mat just see the diffrence between my code and your

did you try my code ?

avatar image polan31 kskjadav007 · Apr 13, 2018 at 11:53 AM 0
Share

Ok, I give myself a last chance.

$$anonymous$$aybe now you'll tell me what I'm doing wrong.

Forgive for speed, but there is a limit to 1 $$anonymous$$ute :)

If you set a large screen, everything will be seen perfectly

link text

Show more comments
avatar image
2

Answer by tormentoarmagedoom · Apr 12, 2018 at 10:17 AM

Good day.

You should first learn how to manage collisions/triggers. I recommend you to create 1 empty object for each color, with a collider in each object, to know what color has been touched.

Look at this tutorial.

Then, you have to change the material of the ball (you must have a material for each color), using Renderer.material.

We can help with some script problem or specific doubt, but you must learn how to use it by your own.

Bye!

Comment
Add comment · Show 4 · 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 polan31 · Apr 12, 2018 at 08:30 PM 0
Share

Ok, if you can explain one thing to me:

Can I use Renderer. $$anonymous$$aterial for 2D objects?

Because as a beginner I tried changing colors in 3D yesterday and I used it there, now I would like to try it in 2D, but unfortunately I do not know how to do it, that's why I asked a question.

avatar image tormentoarmagedoom polan31 · Apr 13, 2018 at 08:00 AM 1
Share

I really have no idea :D I never did 2D things! lets see if someone knows.. :D

avatar image polan31 tormentoarmagedoom · Apr 13, 2018 at 08:31 AM 0
Share

It killed me :)

But thanks for the comment, I appreciate the help :)

avatar image Harinezumi polan31 · Apr 13, 2018 at 09:24 AM 1
Share

Yes, Renderer and $$anonymous$$aterial work the same way in 2D as well. In fact, Renderer is the base class for $$anonymous$$eshRenderer, LineRenderer, SpriteRenderer, and material is a property of Renderer. However, you usually need to use different shaders for your materials for different rendering modes.
Just try the below code to see that it works:

 Renderer r;
 $$anonymous$$eshRenderer m = GetComponent<$$anonymous$$eshRenderer>();
 r = m;
 LineRenderer l= GetComponent<LineRenderer>();
 r = l;
 SpriteRenderer s= GetComponent<SpriteRenderer>();
 r = s;
avatar image
2

Answer by MarioSantoso · Apr 13, 2018 at 09:31 AM

Hi @polan31

All the answers above are pointing on the right direction. My suspect is that your colliders are not working properly.

Since you are playing in 2d, you must have a "Rigidbody 2D" on the circle object, and make sure it has "Circle Collider 2D" as well. As for the rectangles, make sure they have "Box Collider 2D"., the rectangles do not need to have rigidbody.

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
1

Answer by sean244 · Apr 12, 2018 at 08:57 PM

Add a "Target" tag to each colored shaped that the ball can hit. Then add the following code into your ball script

 private void OnCollisionEnter2D(Collision2D other)
 {
     if (other.gameObject.tag == "Target")
     {
         GetComponent<SpriteRenderer>().color = other.gameObject.GetComponent<SpriteRenderer>().color;
     }
 }
Comment
Add comment · Show 5 · 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 polan31 · Apr 12, 2018 at 09:20 PM 0
Share

I tried exactly the same code, only with a different tag ("ColorChanger").

Unfortunately, the code doesn't work in my case.

avatar image sean244 polan31 · Apr 12, 2018 at 09:59 PM 1
Share

You should have 3 different gameobjects in your hierarchy: green, red and pink, each with the “ColorChanger” tag. Also, make sure that these 3 objects are NOT inside another object that has its own boxcollider.

avatar image polan31 sean244 · Apr 13, 2018 at 07:58 AM 0
Share

I've been doing it for two or three days.

In a several combinations: with a smaller - larger box, with and without material etc. etc,

but it doesn't work.

link text

But thanks for the effort you put in to help me

Show more comments
avatar image
1

Answer by waikit97 · Apr 13, 2018 at 03:27 AM

Try creating three empty game objects, only add Box Collider and Sprite Renderer components for them. These three is not necessary to be seen by player.

1) adjust the scale of the box colliders.

The first collider is scaled and placed exactly as the size of the coloured square's green color region, the second collider matches the pink one and the third matches the red one.

2) change the sprite renderer's color as you wish.

Then, change the color in the sprite renderer of each collider object to the color of your coloured square respectively.

3) adding conditions.

Then use the onCollisionEnter method mentioned above by sean to change the ball's color. If it hits the first collider, it changes to green, if it hits the second it changes to pink and if it hits the red one it changes to red.

Comment
Add comment · Show 3 · 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 polan31 · Apr 13, 2018 at 07:59 AM 0
Share

I've been doing it for two or three days.

In a several combinations: with a smaller - larger box, with and without material etc. etc,

but it doesn't work.

link text

But thanks for the effort you put in to help me

avatar image kskjadav007 polan31 · Apr 13, 2018 at 08:48 AM 0
Share

can i see your script ?

avatar image polan31 kskjadav007 · Apr 13, 2018 at 09:06 AM 0
Share

I am sorry, but unfortunately I'm not at home now and I write on the phone from memory :)

But I hope I have not forgotten anything :) If you had any objections, write and I will be sure what and how, when I return home :)

 private void OnCollisionEnter2D(Collision2D other)
     {
         if (other.gameObject.tag == "Target") {
             Debug.Log ("Collision!");
         
             {
 
                 GetComponent<SpriteRenderer> ().color = other.gameObject.GetComponent<SpriteRenderer> ().color;
             }
         }
     }

Each hit was detected.

Follow this Question

Answers Answers and Comments

126 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Collision problem 0 Answers

2D box colliders not touching but are colliding, how to fix? 0 Answers

Weapon System with collide detection (Helps with script pls)!!! 0 Answers

Collision vs trigger 1 Answer

Modular Room System Similar to Mega Man 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