To build upon my script, I got the player to change color after running into an object on the level. Now I am trying to figure out how to get him to change back after lets say 5 seconds. The default color that he starts the game with is white. Be aware, what you are about to see from my failed attempt will be laughable lol.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MorePoints : MonoBehaviour
{
private Renderer rend;
private Color colorToTurnTo = Color.magenta;
private Color colorToTurnToo = Color.white;
public GameObject gameobject;
private float timer = 5f;
private float delay = 5f;
void Start()
{
rend = GetComponent<Renderer>();
rend.enabled = true;
}
private void Update()
{
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.name == "Player1")
{
Destroy(gameObject);
other.GetComponent<Renderer>().material.color= colorToTurnTo;
if (Color.magenta==true)
{
StartCoroutine("StartDelay");
}
}
}
}
IEnumerator StartDelay()
{
Time.timeScale = 5;
Color.magenta = true;
yield return WaitForSeconds(5f);
Color.white = true;
void changeColor()
{
rend.material.color = colorToTurnTo;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MorePoints : MonoBehaviour
{
[SerializeField] private float buffDuration = 5f;
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Player1")
{
PlayerScript player = other.GetComponent<PlayerScript>();
player.MorePointsBuff(buffDuration);
}
}
}
//add the relevant pieces to your player class
public class PlayerScript : MonoBehaviour
{
private MeshRenderer rend;
private void Start()
{
rend = GetComponent<MeshRenderer>();
}
public void MorePointsBuff(float duration)
{
rend.material.color = Color.magenta;
StartCoroutine(SwitchColorBack(duration));
}
private IEnumerator SwitchColorBack(float duration)
{
yield return new WaitForSeconds(duration);
rend.material.color = Color.white;
}
}
}
Also, did this ever happen to you? Backing up your game from a flash drive to C-drive causes one script (just one only lol) to no longer work on certain levels but still works on other levels. Yet, on the flash drives, the script works on all the levels.