How can I correctly generate Button objects with C# script?
I am very new to Unity and am trying to dynamically create Buttons in the form of the periodic table of elements in VR. I am trying to apply what I've learned in this tutorial here Unity Adding Buttons Dynamically Tutorial but the combination of beginner tutorials from Udemy and this official resource is more than confusing me a bit. In my head, I understand the concept programmatically but because I'm new to C# and Unity, I'm having a bit of a tough time getting the ball rolling.
For starters, I have a Button prefab game object that I created with three public variables declared attached to a C# component
Here is the code shown below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TileInformation : MonoBehaviour {
public Button elementButton;
public Text atomicNumber;
public Text elementName;
public void GenerateLabels(string atomicNumber, string elementName) {
atomicNumber.text = atomicNumber;
elementName.text = elementName;
}
}
Next, I have a game object named ElementTileGenerator with a C# script attached. Below is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GenerateElementTile : MonoBehaviour {
public GameObject ElementTile;
private int xPosition = 0;
private int yPosition = 100;
TileInformation tileInfo;
void Start () {
createElementTile ();
}
void Update () {
}
void createElementTile() {
Canvas canvas = (Canvas)GameObject.FindObjectOfType(typeof(Canvas));
GameObject canvasGameObject = canvas.gameObject;
for (int i = 1; i < 119; i++) {
GameObject elementPanel = Instantiate(ElementTile);
elementPanel.transform.parent = canvasGameObject.transform;
elementPanel.transform.localPosition = new Vector3 (xPosition, yPosition, 1);
elementPanel.transform.localScale = new Vector3 (1, 1, 1);
xPosition = xPosition + 50;
if (xPosition >= 1100) {
xPosition = 0;
yPosition = yPosition - 50;
}
}
tileInfo.GenerateLabels ("H", "10");
}
}
Here is the accompanying photo:
When I try to execute this code, I receive the following error:
Assets/ElementVR Files/Scripts/TileInformation.cs(12,16): error CS1061: Type string' does not contain a definition for
text' and no extension method text' of type
string' could be found. Are you missing an assembly reference?
I've looked into this for a few hours and I've come up with nothing to solve my problem. The tutorials I've seen have had this separation of C# components but what I don't get is why can't I just generate the labels in the ElementTileGenerator game object? I've looked into GetComponent, GetComponentInChildren, and similar type methods but I'm definitely not using them right. Could someone help give me a push in the right direction. My goal is just to have 119 tiles that have the Hydrogen label and atomic number 1 just to get this going.
I can add anymore info if its pertinent. Thanks.
Answer by Hellium · Nov 02, 2017 at 08:16 AM
Because you have named the arguments of your function the exact same way as your class' members, you get the error.
public void GenerateLabels(string atomicNumber, string elementName) {
this.atomicNumber.text = atomicNumber;
this.elementName.text = elementName;
}
OR
public Text atomicNumberText;
public Text elementNameText;
public void GenerateLabels(string atomicNumber, string elementName) {
atomicNumberText.text = atomicNumber;
elementNameText.text = elementName;
}