- Home /
Question by
juhaochitudouni · Jul 25, 2021 at 03:20 AM ·
datainstantiate prefab
How to realize the gameobject suddently become a cube just because my string equals to "Cube"
Data is getting from the server. The data contains the object's name and position, then it transforms into a corresponding position in the scene. For example, I get message from json:{"Name":"orange" "Position":"x = 1, y=2"}. I am a beginner at Unity. I am engaged in UI design, and my basic knowledge of code is relatively weak. Please forgive me if it is clearly stated.Thanks a lot !!! using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class SpawnCube : MonoBehaviour
{
public GameObject orange;
public Transform spawnPosition;
public string output;
Vector3 spawnPoint;
void Start()
{
}
void Update()
{
spawnPoint = spawnPosition.position;
string a = GameObject.Find("UIbox2.0/Canvas").GetComponent<GetMQTT>().name;
string b = "orange";
if (String.Equals(string a , string b))
{
NewOrange();
}
}
public void NewOrange()
{
Instantiate(orange, new Vector3(spawnPosition.position.x + 2f, spawnPosition.position.y - 2f, spawnPosition.position.z), Quaternion.identity);
}
}
Comment