i have used an image on my game how can i pop up a new image when the first image is clicked
instead of buttons i have used images..when one image is clicked a new one should popup, can any one provide some refferences
Answer by lawrence-parry · Sep 19, 2016 at 05:36 AM
Is there any particular reason you can't use a button. By default you can assign an image to a button.
Here's a little script I have made that will change the image (you will need to assign 'image' in the inspector).
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ChangeImage : MonoBehaviour {
public Sprite image;
Image imageComponent;
// Use this for initialization
void Start () {
imageComponent = gameObject.GetComponent<Image> ();
}
public void Change()
{
imageComponent.sprite = image;
}
}
You will need to call 'Change()' on the button. So in the OnClick() section choose the object on the left and ChangeImage > Change() on the right.
Hope this helps :)
@lawrence-parry actually i cant use button i have already done many things based on this image, can you provide a reference how a new canvas pops up when one image is clicked
I really don't see why you can't use a button. A button has the very same image component as a regular image.
@lawrence-parry actually am using unity 3d , when i am adding a button it appear like empty am new to this. what i actually want is.... a button for settings, and when button is clicked the setting window should pop up and their should be a option to mute the volume, can you provide me a reference, i will click for your answer
Could you explain a little more about what it is you are after? You want to display a settings window that you have already created when a button is pressed is that right?
@lawrence-parry yes thats right... actualy settings window is a gameobject in which i have added an image
You will probably want to have the settings window disabled by default; uncheck the checkbox at the top left of the inspector when you have your settings object selected. Then enable the object through script when the button is pressed.
using UnityEngine;
using System.Collections;
public class ChangeImage : $$anonymous$$onoBehaviour {
public GameObject settings;
public void Change()
{
settings.SetActive(true);
}
}
You will need to set the settings object for the script in the inspector.
If you want to hide the settings window again use:
settings.SetActive(false);
@lawrence-parry i have a doubt... what i have to do is to make few buttons on the main scene... one setting button ,one shopping button, one mission button, one facebook button...when i click each button new window should come ....how can i do that can you provide me some reference...
one more doubt... if i make 4 separate scene for each buttons and load each scene when each corresponding buttons are clicked is it ok?
hope you will give a suggetion
I wouldn't recommend having separate scenes for when each button is pressed; the game will have to load the new scene when the button is pressed and then load the old one if you switch back again. I think your best bet is enabling and disabling gameobjects. So when one button is pressed you can disable a object containing those buttons (settings, shop, missions etc.) and then enable another object with whatever you now want to be displayed. For example if the settings button is pressed you will disable your object containing the buttons and then enable an object which has your settings menu in it.
P.S. for saving data between scenes create a class, here I'm using one I've called 'GlobalVariables'
public static class GlobalVariables
{
public static string SavedBetweenScenes = "this string can be accessed from any scene"
}
Your answer
Follow this Question
Related Questions
how can i change the color of line renderer 1 Answer
Make simple hit effect 1 Answer
can anyone tell what this script actually does 0 Answers
drag gameobject from ui scroll panel to 3d space 0 Answers
how to make a running player fly through the air at a particular distance from the ground 0 Answers