[Solved ]Modifying only one UI object through scripts

Hello,

I am trying to put a text with black background in front of my UI layer in my game. In order to do that, in my camera canvas object I have created an object called Story_screen, in this object I put an image, this is my black background, inside the Story_screen I have put an object called Story_text, with the text I want to show.

The idea is to show this screen before the level and then the player push a button an the screen should desapear.

I have written the next code attached in an object outside from the camera.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class scr_show_story : MonoBehaviour {
   
    public bool is_pushed = false;
    public float alphaValue = 1.0f;
    public GameObject Story_screen;
    public GameObject Story_text;

    void Start(){
        Story_screen.GetComponent<Image> ().enabled = true;
        Story_screen.GetComponent<Image> ().material.color = new Color (1, 1, 1, 1.0f);
        Story_text.GetComponent<Text> ().enabled = true;
        Story_text.GetComponent<Text> ().material.color = new Color (1, 1, 1, 1.0f);
    }

void Update(){

        if (Input.GetMouseButtonDown (0) || Input.GetButtonDown ("Start") || Input.GetKey(KeyCode.P)) {
            is_pushed = true;
        }

       if (Story_screen.GetComponent<Image> ().enabled && is_pushed && alphaValue > 0.0f) {
            alphaValue -= Mathf.Clamp01 (Time.deltaTime / 0.8f);
            Story_screen.GetComponent<Image> ().material.color = new Color (1, 1, 1, alphaValue);
            Story_text.GetComponent<Text> ().material.color = new Color (1, 1, 1, alphaValue);
        }

}

The trouble is that, when I use this code, not only the Story_screen and its text desapear, every Image and every text in the camara_canvas desapear. I do not understand why because I using
“Story_screen.GetComponent ().material.color = new Color (1, 1, 1, alphaValue);”
it means that only the Image in the “Story_screen” should desapear but it is not so!!!

I hope someone could help me.

Thanks in advance

R.

Ok guys, I got to solve it.

I have changed the next lines

  • Story_screen.GetComponent ().material.color = new Color (1, 1, 1, alphaValue);
  • Story_text.GetComponent ().material.color = new Color (1, 1, 1, alphaValue);

for the followings

  • Story_screen.GetComponent ().color = new Color (1, 1, 1, alphaValue);
  • Story_text.GetComponent ().color = new Color (1, 1, 1, alphaValue);

Note that I have erased the word “material”.

Cheers,

R.