- Home /
Question by
Jaylovegame · Jul 26, 2018 at 06:51 PM ·
spriteslider
spawn a sprite on a UI in C#
hello
So i have a slider handle going from left to right. I want a sprite to spawn where the handle of the slider is when the left mouse button was clicked. All I have is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Axesmarks : MonoBehaviour {
public Sprite woodCrack;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0)){
}
}
}
is there a way to do this?
Comment
Answer by Carterryan1990 · Jul 27, 2018 at 04:17 AM
Add an empty gameobject to the Slider Handle which will be the "SpriteHolder" place the spriteholders transform into the scripts SpriteHolder.
public class Axesmarks : MonoBehaviour {
public Sprite woodCrack;
public Transform spriteHolder;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0)){
Instantiate(woodCrack, spriteHolder.transform.position, Quaternion.identity);
}
}
}
the code is working, but it still not placing anything down. sould i be putting that code in the spriteHolder?
Your answer
![](https://koobas.hobune.stream/wayback/20220612171622im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Slider UI rotate image/sprite 1 Answer
Unity 2D: Design a custom slider object with custom sprites 0 Answers
NGUI Slider foreground color change 1 Answer
Changing UI Slider Art 1 Answer