- Home /
how to set a public GameObject when button clicked
bassically i have a button and when its clicked i want to find the name of the button and set the Selectedobject variable, but im not sure how to approach this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class setSeletedObject : MonoBehaviour {
public string SelectedObject:
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
SetSelectedObject();{
SelectedObject = ;
}
}
Answer by pslattery · Aug 21, 2017 at 06:50 PM
You could have SetSelectedObject take a string argument, and then set the Button's onClick function to pass the string you want. Its not a super robust way of doing this but it would work.
A minor build on this would be to have SetSelectedObject take a GameObject argument and just reference its name in the function. This way if you change the button's name, you don't have to change it in both places.
public void SetSelectedObject (GameObject selectedObject) {
this.SelectedObject = selectedObject.name;
}
Replace your SetSelectedObject code with this. On your Button Component: 1. Click the + next to onClick 2. Drag the GameObject containing your script into the field 3. Find SetSelectedObject in the drop down 4. Drag the object you want to select into the new GameObject field that will appear.
Also, you should use comments to ask for clarification, not answers. Hope this helps!
when i try that i get: failed to convert parameters
After which step (the numbers in my answer above) does that happen?
Answer by YT5h4dow · Aug 21, 2017 at 09:33 PM
is it possible that i could get an example of the code