- Home /
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.
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
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.
Your answer
Follow this Question
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