- Home /
Instantiate problem
I am really new to C# and I am currently working on this AR project which requires me to instantiate an object onto an image target. I am supposed to be able to input the numbers of object in the game itself. My first code works completely fine when I still didn't need to input any number into the game itself yet. using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class CreatingInstance : MonoBehaviour { public GameObject strawberry1; public int input1; public int input2; //private int number1; //private int number2; private int a; private int b; void Start() { //InputField number1 = GameObject.Find("Button").GetComponent<Math>().Field1; //InputField number2 = GameObject.Find("Button").GetComponent<Math>().Field2; //int a = System.Convert.ToInt32(number1); //int b = System.Convert.ToInt32(number2); for (int y = 0; y < input1; ++y) { GameObject go = Instantiate(strawberry1, new Vector3(0, y, 0), Quaternion.identity) as GameObject; go.transform.parent = GameObject.Find("4strawberries").transform; } } }
However, when I tried to upgrade the code to be able to input and do some simple addition in the game itself, the instantiated position of my object goes completely off and I couldn't figure out what is the issue. Here is my second code.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class Math : MonoBehaviour
{
public GameObject strawberry1;
public InputField Field1;
public InputField Field2;
public Text Result;
public void Sum()
{
int a = System.Convert.ToInt32(Field1.text);
int b = System.Convert.ToInt32(Field2.text);
int c = a + b;
Result.text = c.ToString();
for (int y = 0; y < a; ++y)
{
//GameObject go = Instantiate(strawberry1, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
GameObject go = Instantiate(strawberry1, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
go.transform.parent = GameObject.Find("Strawberries").transform;
}
}
}
Would really appreciate it if someone could help me figure out what is the issue. Thank you very much.
Your answer
![](https://koobas.hobune.stream/wayback/20220613022600im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Create random order and go through till the end (C#) 1 Answer
Is there anyway to delete clones that are local variables? 1 Answer
How to make TCG Deck (Instatiate based on Prefab) 1 Answer
How to instantiate platforms like Doodle Jump game ? 1 Answer
How can I Instantiate two new GameObjects at the same time? (Attempting Asteroids style game) 2 Answers