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 /
avatar image
0
Question by Supermacka · Jun 21, 2018 at 10:33 PM · color changeoncollision

Color not changing on collision

Hi fellow better programmers,

I am working on my first game and stumbeld across this problem where the color of the other players tiles wont change when shot.

alt text

alt text

As you can see on the second picture the "red player" has shot the "blue player" but has not changed the tiles below him to red.

Here is the code (the first one for the "red player" and the second for the "blue player"):

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Player_1_Coloring : MonoBehaviour
 {
     public float countRed;
     
     public void Start()
     {
         
     }
 
     public void OnCollisionEnter(Collision col)
     {
         if (col.gameObject.tag == "ColorFloor" && col.gameObject.GetComponent<Renderer>().material.color == Color.blue)
         {
             GameObject.Find("Player 2").GetComponent<Player_2_Coloring>().countBlue -= 1;
         }
         if (col.gameObject.tag == "ColorFloor" && col.gameObject.GetComponent<Renderer>().material.color == Color.white || col.gameObject.tag == "ColorFloor" && col.gameObject.GetComponent<Renderer>().material.color == Color.blue)
         {
             col.gameObject.GetComponent<Renderer>().material.color = Color.red;
             countRed += 1;
         }
     }
 }



 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Player_2_Coloring : MonoBehaviour
 {
     public float countBlue;
 
     public void Start()
     {
     }
 
     private void Update()
     {
     }
 
     public void OnCollisionEnter(Collision col)
     {
         if (col.gameObject.tag == "ColorFloor" && col.gameObject.GetComponent<Renderer>().material.color == Color.red)
         {
             GameObject.Find("Player 1").GetComponent<Player_1_Coloring>().countRed -= 1;
         }
         if (col.gameObject.tag == "ColorFloor" && col.gameObject.GetComponent<Renderer>().material.color == Color.white || col.gameObject.tag == "ColorFloor" && col.gameObject.GetComponent<Renderer>().material.color == Color.red)
         {
             col.gameObject.GetComponent<Renderer>().material.color = Color.blue;
             countBlue += 1;
         }
 
     }
 }

And here is the code for when a player is shot (1 for "red player" and 2 for the "blue player"):

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Player1_Spawning : MonoBehaviour
 {
     public GameObject player1;
     public Transform Player1_Spawn;
     public GameObject Shots1;
 
     // Use this for initialization
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {
     }
 
     private void OnCollisionEnter(Collision col)
     {
         if (col.gameObject.tag == "Shot2")
         {
             gameObject.SetActive(false);
             Invoke("Spawn1", 3f);
             Shots1.SetActive(false);
 
         }
     }
 
     public void Spawn1()
     {
         player1.SetActive(true);
         player1.transform.position = Player1_Spawn.transform.position;
         player1.transform.rotation = Player1_Spawn.transform.rotation;
         Shots1.SetActive(true);
     }
 }



 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Player2_Spawning : MonoBehaviour
 {
     public GameObject player2;
     public Transform Player2_Spawn;
     public GameObject Shots2;
 
     // Use this for initialization
     void Start()
     {
     }
 
     // Update is called once per frame
     void Update()
     {
     }
 
     private void OnCollisionEnter(Collision col)
     {
         if (col.gameObject.tag == "Shot1")
         {
             gameObject.SetActive(false);
             Invoke("Spawn2", 3f);
             Shots2.SetActive(false);
         }
     }
 
     public void Spawn2()
     {
         player2.SetActive(true);
         player2.transform.position = Player2_Spawn.transform.position;
         player2.transform.rotation = Player2_Spawn.transform.rotation;
         Shots2.SetActive(true);
     }
 }

Thanks in advance <3

the-game11.png (196.8 kB)
the-game22.png (195.9 kB)
Comment
Add comment · Show 1
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 losingisfun · Jun 22, 2018 at 05:11 AM 0
Share

looks like a neat little game!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by losingisfun · Jun 22, 2018 at 05:10 AM

There are some things here I would highly recommend fixing. Such as:

 GameObject.Find("Player 1").GetComponent<Player_1_Coloring>().countRed -= 1;

This is an extremely expensive way to change an integer value, consider making countRed a static value that you can change more easily. Such as: public static int countRed = 0; Then you only need to write Player_1_Colouring.countRed -= 1; Because Find and GetComponent are very heavy and should not be used often or at all if avoidable.

Secondly, you should always consider cacheing components instead of using GetComponent multiple times for the same component. such as:

 Renderer ren = col.gameObject.GetComponent<Renderer>();

then check it from there on, like if (ren.material.color == ...) etc.

This will cut back the cost of your performance... like a hundred times over.

To answer your question, I'd say the floor isn't changing color because perhaps you are destroying the projectile after it hits the player, and before it ever reaches the ground. But that's probably in a different part of your code that you haven't shown.

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

84 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

Related Questions

Is there a function for OnCollision, that detects drag? 0 Answers

How to get the collided plane of a mesh, not just the object,How to get the collided plane of a mesh 1 Answer

How change slider(prefab) fill color in a script? 1 Answer

How can i change numbers by time 3 Answers

Buttons will not change color 11 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