Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Something obvious that i'm not able to see

Discussion in 'Scripting' started by Onilut, Sep 16, 2015.

  1. Onilut

    Onilut

    Joined:
    Aug 10, 2015
    Posts:
    36
    I have a problem... basically, i cant modify the alpha of a text... and i Don't know the reason... i've tried a lot of different ways of writting the same code to do exactly the same, but just i can't... the only thing i want to do is to make the text invisible in a fixed time. but i have an error that dont let me build the code.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class class_PointsMade : MonoBehaviour {
    6.  
    7.     public GameObject objetoPuntos;
    8.     public Text textPoints;
    9.  
    10.     float alpha = 0f;
    11.  
    12.     float tiempo = 0f;
    13.  
    14.     void Update()
    15.     {
    16.         tiempo+= Time.deltaTime;
    17.         this.transform.Translate(1f * Time.deltaTime, 1f * Time.deltaTime, 0f, Space.World);
    18.  
    19.         if (tiempo >= 2f)
    20.             Destroy(this.gameObject);
    21.  
    22.         alpha = Mathf.Lerp(0f, 1f, Time.deltaTime);
    23.  
    24.         textPoints.color.a = alpha;
    25.  
    26.  
    27.     }
    28. }
     
  2. Onilut

    Onilut

    Joined:
    Aug 10, 2015
    Posts:
    36
    and here the error Assets/Scripts/class_PointsMade.cs(24,28): error CS1612: Cannot modify a value type return value of `UnityEngine.UI.Graphic.color'. Consider storing the value in a temporary variable. It must be something really simple... but i just cant see what it is...
     
  3. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Try:

    Code (CSharp):
    1. Color textcolor = textPoints.color;
    2. textcolor.a = alpha;
    3. textPoints.color = textcolor;
    The reason for this is that color is probably generated by some logic and doesnt actually have a backing variable. If you do this it will use the new color fine.
     
  4. MikeUpchat

    MikeUpchat

    Joined:
    Sep 24, 2010
    Posts:
    1,055
    You need to get the textPoints.Color, change the alpha component and then set it back ie:
    Code (csharp):
    1. Color col = textPoints.color;
    2. col.a = alpha;
    3. textPoints.color = col;
     
  5. EETechnology

    EETechnology

    Joined:
    Aug 15, 2015
    Posts:
    185
    If you want to not see it, you could deactivate it, for this I think I have the solution :
    Code (CSharp):
    1. Color color = textPoints.color;
    2. color.a = ....;
    3. textPoints.color = color;
     
  6. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    He wants it to slowly fade

    In C# there are some cases, where you can't just simply set one of the variables, examples like this is transform.postion/rotation, rigidbody.velocity etc. You need to set the whole thing at once or use a temporarly variable
     
  7. EETechnology

    EETechnology

    Joined:
    Aug 15, 2015
    Posts:
    185
    I already told him to do so. Look at my script :)
     
  8. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
  9. Onilut

    Onilut

    Joined:
    Aug 10, 2015
    Posts:
    36
    Ye, I tried that. see
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class class_PointsMade : MonoBehaviour {
    6.  
    7.     public GameObject objetoPuntos;
    8.     public Text textPoints;
    9.  
    10.     Color color = textPoints.Color;
    11.  
    12.     float alpha = 0f;
    13.  
    14.     float tiempo = 0f;
    15.  
    16.     void Update()
    17.     {
    18.         tiempo+= Time.deltaTime;
    19.         this.transform.Translate(1f * Time.deltaTime, 1f * Time.deltaTime, 0f, Space.World);
    20.  
    21.         if (tiempo >= 2f)
    22.             Destroy(this.gameObject);
    23.  
    24.         alpha = Mathf.Lerp(0f, 1f, Time.deltaTime);
    25.  
    26.         //textPoints.color.a = alpha;
    27.  
    28.  
    29.     }
    30. }
    and this error: Assets/Scripts/class_PointsMade.cs(10,23): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `class_PointsMade.textPoints'

    for some reason, i cant access to textPoints.color to make the reference to color
     
  10. MikeUpchat

    MikeUpchat

    Joined:
    Sep 24, 2010
    Posts:
    1,055
    You can't in C#, it is allowed in Java to directly set the components but not in C#, you have to get a copy of the values, then set the component you want then write it back as is shown multiple times above.
     
  11. Onilut

    Onilut

    Joined:
    Aug 10, 2015
    Posts:
    36
    I have some problems making that... I have seen other Scripts ive made, and they work

    text.color = Color.Lerp (text.color, Color.clear, Time.deltaTime * 3);

    text was the text variable of type Text... I saw this code made on C# and it works, the text dissapear.

    but in my class_Pointsmade that i shown before, dont allow me to write exactly the same...

    This class is inside a prefab like this: https://gyazo.com/79a0c6cd604b533f7f466958f4176a4f

    that gameobjects are instantiated CORRECTLY on screen with a game controller script... and i had no problem changing the text and the color in this "game controller"... so... what i'm missing :(
     
  12. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    In case you're talking about JavaScript (UnityScript), that's not the same as Java.


    What you're missing is the difference between value types (struct in this case) and reference types, and what exactly happens when you "copy" a variable of them or to be more precise in this case, what happens when structs are used in conjuction with properties. (text.color is a property).
     
  13. MikeUpchat

    MikeUpchat

    Joined:
    Sep 24, 2010
    Posts:
    1,055
    Since we are in a Unity forum and talking about scripting I was assuming people would understand Java meant Javascript :)
     
  14. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class class_PointsMade : MonoBehaviour {
    6.  
    7.     public GameObject objetoPuntos;
    8.     public Text textPoints;
    9.  
    10.     Color color = textPoints.Color;
    11.  
    12.     float alpha = 0f;
    13.  
    14.     float tiempo = 0f;
    15.  
    16.     void Update()
    17.     {
    18.         tiempo+= Time.deltaTime;
    19.         this.transform.Translate(1f * Time.deltaTime, 1f * Time.deltaTime, 0f, Space.World);
    20.  
    21.         if (tiempo >= 2f)
    22.             Destroy(this.gameObject);
    23.  
    24.         alpha = Mathf.Lerp(0f, 1f, Time.deltaTime);
    25.  
    26.         color.a = alpha;
    27.        textPoints.color = color;
    28.  
    29.  
    30.     }
    31. }
     
  15. Onilut

    Onilut

    Joined:
    Aug 10, 2015
    Posts:
    36
    I can't write Color color = textPoints.color; it gives me an error that I posted before: "A field initializer cannot reference the nonstatic field, method, or property `class_PointsMade.textPoints
    So... What I need to do? I have a Gamecontroller empty on the hierarchy that instatiate this points prefabs... there I could modify thetextPoints.color without any problem...
    Code (CSharp):
    1. GameObject newPointsMade = Instantiate (objetoPuntos) as GameObject;
    2.         class_PointsMade pointsObject = newPointsMade.GetComponent <class_PointsMade>();
    3.         pointsObject.textPoints.text = textPoints;
    4.         pointsObject.textPoints.color = colorPoints;
    5.         newPointsMade.transform.SetParent(myPanel);
    6.         newPointsMade.transform.localScale = new Vector3 (1f, 1f, 1f);
    7.         newPointsMade.transform.position = positionPoints;
    "
     
  16. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    You cant do that as a field initializer

    This means you cant initialize it in the class definition like that That is reserved for using new/static/value types. You need do it in a constructor or because it is a MonoBehaviour you need to do it in Awake/Sleep. Like this

    Code (CSharp):
    1.  
    2. void Start()
    3. {
    4. color = texPoints.color;
    5. }
     
  17. Onilut

    Onilut

    Joined:
    Aug 10, 2015
    Posts:
    36
    I solved it adding thet Start() function. I still don't know the reason why this worked and the other not.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class class_PointsMade : MonoBehaviour {
    6.  
    7.     public GameObject objetoPuntos;
    8.     public Text textPoints;
    9.  
    10.     Color color;
    11.  
    12.     float alpha;
    13.  
    14.     float tiempo = 0f;
    15.  
    16.     void Start ()
    17.     {
    18.         alpha = 1f;
    19.         color = textPoints.color;
    20.     }
    21.  
    22.     void Update()
    23.     {
    24.         tiempo+= Time.deltaTime;
    25.         this.transform.Translate(1f * Time.deltaTime, 1f * Time.deltaTime, 0f, Space.World);
    26.  
    27.         if (tiempo >= 2f)
    28.             Destroy(this.gameObject);
    29.  
    30.         alpha = Mathf.Lerp (alpha, 0f, Time.deltaTime * 1f);
    31.  
    32.         color.a = alpha;
    33.  
    34.         textPoints.color = color;
    35.  
    36.  
    37.         //textPoints.color.a = alpha;
    38.  
    39.  
    40.     }
    41. }
    Making the initialization on Start() works fine...

    in alpha =Mathf.Lerp(0f, 1f, Time.deltaTime);
    it makes an interpolation between 0f and 1f, based on t (Time.deltaTime in this case). But it makes the same interpolation every frame... so if you have alpha = Mathf.Lerp(0f, 1f, 0.5f); the result is always going to be alpha= 0.5f and it never reachs 1f. So i had to make this alpha = Mathf.Lerp (alpha, 0f, Time.deltaTime); so it always start on the value on alpha... The problem with this is it makes not a linear interpolation. So if some1 knows how to make a REAL linear interpolation, please reply this.

    Thanks for your Help people... You are the Best :D

    Edit: Thnaks Korno for your help and the explanation... I did the start() (just because I was trying a lot of different things) and I didn't know Why it works
     
  18. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You have to increase the value for t, while start and end stays the same. t=0 returns the start value, t= 1 the target value. So the simplest way to make this work is having a variable starting with 0 and add Time.deltaTime to it in every frame.