Search Unity

[Solved] Change the clone material ?

Discussion in 'Scripting' started by Quast, Mar 19, 2018.

  1. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    Hi,
    I have issue that i can't change the clone material.

    Code (CSharp):
    1.     public GameObject prefab;
    2.     public Vector3 desroyposition;
    3.     Color object_color;
    4.     public bool color_check;
    5.     // Use this for initialization
    6.     void Start () {
    7.  
    8.         color_check = false;
    9.  
    10.  
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update () {
    15.        
    16.         desroyposition = gameObject.transform.position;
    17.  
    18.         if (color_check == true) {
    19.             brok();
    20.         }
    21.  
    22.     } // end update
    23.  
    24.     public void brok(){
    25.         GameObject ball1 = Instantiate(prefab) as GameObject;
    26.         ball1.transform.position = new Vector3(desroyposition.x,desroyposition.y - 1,desroyposition.z);
    27.         object_color = ball1.GetComponentInChildren<Renderer>().material.color;
    28.    
    29.         object_color = new Color(object_color.r, object_color.g, object_color.b, 10f);
    30.         ball1.GetComponentInChildren<Renderer>().material.color = object_color;
    31.        
    32.         Destroy(ball1, 9f);
    33. }
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Your "brok" method requires "color_check" to be true in order for it to be called. There is no line/condition in this script that sets this variable to true.
     
  3. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    Yes, there is line 18, and i do that manually.
    I need to know why material not fade ?
     
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    After looking through the method, I think this line is the problem:
    Code (CSharp):
    1. object_color = new Color(object_color.r, object_color.g, object_color.b, 10f);
    Because the alpha value has a minimum of 0 and a maximum of 1 (same for r, g, & b values). You need to set the value to a number less than 1 to decrease the opacity of the object.
     
  5. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    I changed the value from 10f to 0.3f. but nothing change ?
    What else can i do to solve this ?
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    It's working for me.

    Also note that you could write this:
    Code (csharp):
    1. object_color.a = .3f;
    2. ball1.GetComponentInChildren<Renderer>().material.color = object_color;
    3. // in fact you could write this..
    4. Renderer r = ball1.GetComponentInChildren<Renderer>();
    5. object_color = r.material.color;
    6. object_color.a = .3f;
    7. r.material.color = object_color;
    Just a little different ;) (no affect on working/not, though..)
     
  7. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    If the code is working for @methos5k, then the only thing I can think of is this:
    You're spawning a new ball once every frame. If the positions of these balls do not change from when they were spawned, then each new ball would be spawning & staying inside the previous ball. Even though the balls are transparent, with so many of them stacked inside of each other, it would give off the illusion that there is one solid-colored ball.
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You know, I was thinking the same thing. My post originally included "Are you just spawning 1 object?"..
    but I erased it :(

    Of course my code was slightly modified, because I don't have all the same settings.. but the relevant portion was working ;)
     
  9. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    Now i understand why it's not working. There is misunderstand. I'm not using the child object as on the image above. I'm using the parent object. Yes, if i take the child it will work.

    I'm trying to make all objects with the parent to change it own material. How to do that ?
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Just to be clear.. you instantiate a copy of the parent, and then want to modify the colour on each child?
     
  11. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    Yes.
    Also, parent and child objects sharing the same material.
     
  12. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, well 1 thing is you'd need to use the plural of get component. Previously we were getting only 1 :)
     
  13. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    OK, you right. Thank you all for what you did for me.
     
  14. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    No problem. :) Take it easy.
     
    Quast likes this.