- Home /
Change opacity of instantiated object at collision
Hey, In my scene I have a Spawner gameobject that shoots Enemy gameobject. On a certain condition I want both the Enemy and the Player to change opacity for short time.
Enemy is accessed by three scrips: - SpawnerController : handles all enemies in the level. (speed, instantiation, etc) - Mover : handles enemies individually - OnCollision : Checks collision between enemy object and player
Problem is that in the game several enemies are going to be instantiated and have the mover script attached and to be able to acces the material from any other script I'm going to have to assign the gameObject in the inspector. Which isn't going to work if I have 20 "different" enemy objects in the game.
Ideally I would like to have the enemy change collision by using other.LightFade() in the OnCollision script , so the LightFade method gets activated in the Mover script
But because of the mentioned problem I get the error
" Type
UnityEngine.Collider' does not contain a definition for
Mover' and no extension methodMover' of type
UnityEngine.Collider' could be found. Are you missing an assembly reference?"*
Is there any clean solution or work-around to this problem?
Here are the related scripts:
OnCollision: public class OnCollision : MonoBehaviour {
//On each collision the player makes with an enemy, the system checks the current texture of the player determining it's current status
//Then checks the tag associated with the enemy and decides if it's a win or a loss. Nothing happens if the player is equal to the enemy.
public Renderer currentMode; //Stores the current status derived from the player renderer. Gets updated on each collision.
public GameController GameController;
public PlayerController PlayerController;
void OnTriggerEnter (Collider other)
{
currentMode = this.GetComponent<Renderer>();
if((currentMode.material.mainTexture.name == "PlayerRock" && other.gameObject.tag == "Enemy_Paper")||(currentMode.material.mainTexture.name == "PlayerPaper" && other.gameObject.tag == "Enemy_Scissor")||(currentMode.material.mainTexture.name == "PlayerScissor" && other.gameObject.tag == "Enemy_Rock"))
{
GameController.LoseLife();
PlayerController.GetHit();
}
if((currentMode.material.mainTexture.name == "PlayerRock" && other.gameObject.tag == "Enemy_Scissor")||(currentMode.material.mainTexture.name == "PlayerPaper" && other.gameObject.tag == "Enemy_Rock")||(currentMode.material.mainTexture.name == "PlayerScissor" && other.gameObject.tag == "Enemy_Paper"))
{
GameController.AddScore();
Destroy(other.gameObject);
}
else if ((currentMode.material.mainTexture.name == "PlayerRock" && other.gameObject.tag == "Enemy_Rock")||(currentMode.material.mainTexture.name == "PlayerPaper" && other.gameObject.tag == "Enemy_Paper")||(currentMode.material.mainTexture.name == "PlayerScissor" && other.gameObject.tag == "Enemy_Scissor"))
{
PlayerController.LightFade();
other.LightFade();
}
}
}
SpawnerController:
public class SpawnerController : MonoBehaviour
{
//The Spawner game object manages the instantiation of the enemies
// Array of GameObject prefabs set in the Engine/Inspector
public GameObject[] mode;
// Spawntime settings
public int spawnInterval;
public float startWait;
public float moverEnemySpeed;
// Runs on start-up game
void Start ()
{
spawnInterval = 1;
startWait = 1;
moverEnemySpeed = 2f;
//Repeats a function on declared start-up time startWait and with set interval spawnInterval
InvokeRepeating("Spawn",startWait, spawnInterval);
}
public void StopSpawning ()
{
CancelInvoke("Spawn");
}
public void DeleteEnemies ()
{
foreach (Transform child in transform)
{
GameObject.Destroy(child.gameObject);
}
}
//Spawns the enemies
void Spawn ()
{
//Randomises a spawnposition along the x-axis
Vector3 position = new Vector3(Random.Range(-3.0f,3.0f), 0, 8.0f);
//By default the enemy planes are standing upright and invisible for the top down camera, rotating them 90 degrees on creation fixes that
Quaternion spawnRotation = Quaternion.Euler(90,0,0);
//Picks a random enemy from the array and instantiates it in the game
(Instantiate(mode[Random.Range (0,mode.GetLength (0))],position, spawnRotation) as GameObject).transform.parent = this.transform;
}
}
Mover: public class Mover : SpawnerController { //This script is applied to all instantiated enemy player to make them move downward the screen
public Color c;
public Renderer rend;
//Rigidbodies are needed to move the enemy
private Rigidbody rb;
// Runs on startup game
void Start ()
{
rend = GetComponent<Renderer>();
rb = GetComponent<Rigidbody>();
c = rend.material.color;
c.a = 1f;
rend.material.color = c;
//rb.velocity = -transform.up * moverEnemySpeed; //Starts movement of enemies
rb.velocity = -transform.up * (transform.parent.GetComponent<SpawnerController>().moverEnemySpeed);
}
public void LightFade()
{
StartCoroutine(LightFadeOut());
StartCoroutine(LightFadeIn());
}
IEnumerator LightFadeOut()
{
for(float k = 1f ; k >= 0.5f; k -=0.1f)
{
this.c.a = k;
this.rend.material.color = c;
yield return new WaitForSeconds(0.1f);
}
}
IEnumerator LightFadeIn()
{
for( float l = 0.5f ; l <= 1f; l +=0.1f)
{
this.c.a = l;
this.rend.material.color = c;
yield return new WaitForSeconds(0.1f);
}
}
}