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