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 Rizelyn · Jul 07, 2018 at 10:14 AM · collisionmaterialinspectorotheropacity

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 method Mover' 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);
     }
  }
 

}

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

149 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 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

How to change a texture's (or a material's ?) opacity ? 1 Answer

problem with collision and trigger 1 Answer

Change the material ELEMENT on an Object in a script? 1 Answer

OnCollisionEnter is slow... 2 Answers

Edit chosen material in the inspector for custom editor. 2 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