how can i make an image appear?

Hi! i am new with this program and i was doing the ball tutorial and i would like to know how can i make an image appear when i pick up all the balls? Not just a text. Using script

thx :slight_smile:

this is the script i’m using:

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

public class PlayerController : MonoBehaviour {

public float speed;
public Text countText;
public Text winText;
private Rigidbody rb;
private int count;
void Start ()
{
     rb = GetComponent<Rigidbody>();
     count = 0;
     SetCountText ();
     winText.text = "";
}
void FixedUpdate ()
{
     float MoveHorizontal = Input.GetAxis ("Horizontal");
     float MoveVertical = Input.GetAxis ("Vertical");
     Vector3 movement = new Vector3 (MoveHorizontal, 0.0f, MoveVertical);
     rb.AddForce (movement * speed);
}
void OnTriggerEnter(Collider other)
{
     if (other.gameObject.CompareTag ("Pick Up"))
     {
         other.gameObject.SetActive (false);
         count = count + 1;
         SetCountText ();
     }
 
}
void SetCountText ()
{
     countText.text = "Pick Up Objects: " + count.ToString ();
     if (count >= 7)
     {
         winText.text = "You Win!";
     }
}
}

Please, use tags for code Using code tags properly

Ok, did It… now please, help :frowning:

If you’re talking about displaying a flat UI type image, you can have a raw image component set with the texture you want displayed, and have it attached to an inactive gameobject. Whenever you want to display the image you just enable the gameobject. When you want the image to go away you just disable it.

https://docs.unity3d.com/Manual/script-RawImage.html

If that isn’t what you meant, then please be more descriptive.

like the tutorial, when you finish collect all pick up objects, a text appears “you win”
i want the same thing to happens, but with an image to show up when i finish collecting all objects.
so my question is: how can i do it with script?

Put image on scene, SetActive it to false and Active it in whenever you want.
You can also add some animation to it if you want.

[SerializeField] Image showImage;
int maxPickUps = 7; // set this to amount of your pickups

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Pick Up"))
        {
            other.gameObject.SetActive(false);
            count = count + 1;
            CheckPickUps();
        }

    }
    void CheckPickUps()
    {
        if (count >= maxPickUps)
        {
            showImage.SetActive(true);
        }
    }

This happens:
3330548--259576--upload_2017-12-21_18-56-2.png

:frowning:

Use “enabled = true” instead of SetActive.

SetActive is for GameObjects. Components (Like UnityEngine.UI.Image) are enabled/disabled via component.enabled = .

Ah, yeah, sorry for that mistake ;d
You can use enable as roykoma suggest or showImage.gameObject.SetActive(condition);

It’s not working, guys… :frowning:

i really suck at this… :frowning:
should be like this: ?

public class PlayerController : MonoBehaviour {

    public float speed;
    public Text countText;
    public Text winText;
    public Image Image;

    private Rigidbody rb;
    private int count;

    [SerializeField] Image showImage;
    int maxPickUps = 7;

    void Start ()
    {
        rb = GetComponent<Rigidbody>();
        count = 0;
        SetCountText ();
        winText.text = "";
    }

    void FixedUpdate ()
    {
        float MoveHorizontal = Input.GetAxis ("Horizontal");
        float MoveVertical = Input.GetAxis ("Vertical");

        Vector3 movement = new Vector3 (MoveHorizontal, 0.0f, MoveVertical);

        rb.AddForce (movement * speed);
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag ("Pick Up"))
        {
            other.gameObject.SetActive (false);
            count = count + 1;
            SetCountText ();
            CheckPickUps ();
        }
      
    }
    void CheckPickUps()
        {
        if (count >= maxPickUps)
        {
            enabled = true;
        }
    }
if (count >= maxPickUps)
{
   enabled = true;
}

Just “enable” is referencing to actual script, you want to reference to showImage, so

showImage.enabled = true;
1 Like

Ok, no error, nothing, but the image is always up, i want it to appear when i finish picking up all object:face_with_spiral_eyes:

do i have to add a script component to the image itself?

anyway, this appears when i try to play:

3332152--259795--saveme.png

oh, also this: :face_with_spiral_eyes:

Please, think for a moment. Your image is always up because you didn’t deactived it… Deactive manually or even better put in Awake/Start method [quote]
showImage.enabled = false;
[/quote]
Also, you have to assign this image to showImage in inspector.

1 Like

Yup, i had to deactivate or use that code “showImage.enabled = false;”

as i said before, im new with this :confused: but, thank you for the patience, you were great! ^^

Could you help me again, @Prastiwar ? i’m doing the “roguelike 2d” tutorial, everything was fine till i got an error with a code “gameover”. To make sure i was doing nothing wrong, i “ctrl c + ctrl v” the tutorial script and samething happens. Take a look:


what can i do?
Also, i “ctrl c ctrl v” the GameManager script…

Errors usually say everything you need to know what’s going wrong. So, it’s telling you that you don’t have “GameOver” method there is no something like void GameOver()) in GameManager script.

i used the tutorial script, how can i fix it? i dont understand this C language. i have to type in the gamemanager script that void code? if yes, ok, but i dont understand what exactly i have to write >.<

You can fix this by watching this tutorial once again and check if you have everything okey.

  • 0.45 sec it’s it I believe