How can I display an image/ video from the script ?
Hi, I am a new user and would like to know how to display an image in the scene from the script. I have a picture series in a folder on my computer and wish to display an image at a specific time. When the player shoots the displayed image I want to remove it and display another one. Is it possible to do this under Unity? If so, would it be possible to help me? Thank you
Can you post some code and details about your scene structure for people to be able to help you?
Hello, Thank you for your reply. Indeed, I try to adapt an existing game (Unity Cookie 2D Space Shooter) to create one according to my needs (see attached file). I want to display a spaceship in the middle of my scene and display rocks, the player has to shoot the rocks and when the rock disappears I want to display at the same location of the rock an image among the images present on my disc. I would like to know how I can do that? Any help / direction is welcome. Thank you @$$anonymous$$entoliptus
Answer by Mentoliptus · May 17, 2017 at 06:42 AM
There a multiple ways of achieving what you described, but I think it is best to do it with prefabs. If you are not familiar with them, look in the Unity manual and in the Unity tutorials to get the basics.
First create a prefab with an Image component that is your desired image from disk.
Then declare a class variable for your prefab (it can be a specific type or just GameObject) and add the creation of you prefab in the DestroyRock() method. Something like this:
public GameObject destroyedRockPrefab;
IEnumerator DestroyRock()
{
gameManager.GetComponent<GameManager>().UpdateScore(score);
yield return new WaitForSeconds(0.2f);
// instantiate prefab on the same coordinates as the destroyed rock
var destroyedRock = Instantiate(destroyedRockPrefab, gameObject.transform.position, gameObject.transform.rotation);
Destroy(gameObject);
}
Here are some more resources about instantiating prefabs at runtime: Unity manual and Unity scripting reference. I hope they will help you with your game.
Let me mention that I did the same thing for my first game, took one finished game from a tutorial and modified it to meet my vision. Line by line you learn and will be able to create a game from scratch. Good luck!
Answer by ridet · Jun 12, 2017 at 10:45 PM
Hello Mentoliptus Thank you very much for your light, it works very well. I have a second question if you can help me this would be very appreciated. Instead of the images I would like to display small video in .mov format. I exported them into my asset folder and add a Gameobject> UI> RawImage with the RawImage texture video. However I would like to display the video where the rock is displayed as the last time. I can not put the canvas / RawImage in the same place of rock when it disappears. Would you know how I could do it? Any help will be welcome. Thanks again in advance for your help. @Mentoliptus
[1]: /storage/temp/95836-c1.txt
Hi @ridet , sorry for the late replay, but I will have no time to be online for a week or two (my son was born a few days ago) so I suggest that you post this as a new question so others can answer it. Please tag me in the new question so if nobody answers you question, I'll answer it as soon as I can. Thank you your understanding. Good luck!
Your answer
Follow this Question
Related Questions
SetActive dosen't work 0 Answers
File.WriteAllBytes problem 0 Answers
onClick Event for UI Text? 1 Answer
Image not appearing after transferring build to Mac from PC? 0 Answers
ERROR CS0119 C# GetComponent< > 1 Answer