- Home /
when I click a button I want it to move to one of my spawners
When I click my button I want it to go to a random one of my 21 spawners on my canvas.
Thanks in advance
I don't really understand what you want, the button is in the inspector or in the canvas ? The spawner are in the world or in the canvas ? Do you want to move the editor camera or a camera in the game ? Or you want to position the spawner somewhere in the scene ?
I guess this can help you, but who knows ^^ https://answers.unity.com/questions/942869/move-scene-camera-to-gameobject-via-editor-script.html
@fmarianacci Basically in the canvas I have all my stuff and I just want my button to reposition when you tap it it’s all in the canvas and I don’t want anything else to move just the button to one of my 21 spawners thanks
Any screenshot of what you need may help me to understand :)
@fmarianacci the 21 spawners are the things i want to relocate to when i tap the tile (button as called in the game) But i want it to be a random one :)
Answer by leftshoe18 · Dec 09, 2018 at 01:53 PM
Create a method that moves the button and make sure it's public:
public void MoveButton()
{
int rand = Random.Range(0, spawnerArray.Length)
button.transform.position = spawnerArray[rand].transform.position;
}
Then assign this method to the OnClick() property of your button.
Just to make sure my spawners are just empty objects that I have called spawner so do I have to add code to make the empty object a spawner
Apply the script to your container of spawner objects. I would store them as a Transform or GameObject array.
Full script:
using UnityEngine;
public class SpawnController : $$anonymous$$onobehaviour
{
// put all your spawner items in here
[SerializeField] Transform[] spawnerArray;
public void $$anonymous$$oveButton()
{
int rand = Random.Range(0, spawnerArray.Length)
button.transform.position = spawnerArray[rand].position;
}
}
Your answer
Follow this Question
Related Questions
2d look at workaround creates jitter 0 Answers
Get object to follow line created by trail renderer 1 Answer
Destroy script turns objects into ghosts 2 Answers
How to check if an object hits the ground hard enough then add explosive force around it (2D) 1 Answer
Instantiate a GameObject with a specific Z rotation 2 Answers