- Home /
Question by
ChocoboScribe · Jul 17, 2020 at 03:01 AM ·
c#instantiateprefabsparent
Trying to instantiate prefabs to a parent object with C# script.
I'm trying use C# to instantiate a prefab to a parent object for a goals panel for a 3-match puzzle game. However, when I hit play the object isn't created. In the attached objects, the first image is what I see when I hit play and the second is an idea of what I want to happen.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class BlankGoal
{
public int numberNeeded;
public int numberCollected;
public Sprite goalSprite;
public string matchValue;
}
public class GoalManager : MonoBehaviour
{
public BlankGoal[] levelGoals;
public GameObject goalPrefab;
public GameObject goalIntroParent;
public GameObject goalGameParent;
// Start is called before the first frame update
void Start()
{
SetupIntroGoals();
}
void SetupIntroGoals()
{
for(int i = 0; i > levelGoals.Length; i++)
{
// create a new Goal Panelat the goalIntroParent Position
GameObject goal = Instantiate(goalPrefab, goalIntroParent.transform.position, Quaternion.identity);
goal.transform.SetParent(goalIntroParent.transform);
}
}
// Update is called once per frame
void Update()
{
}
}
goal-panel-unity-2.png
(194.3 kB)
annotation-2020-07-16-195628.png
(140.5 kB)
Comment
can you change the code to this and test?
void SetupIntroGoals()
{
for(int i = 0; i > levelGoals.Length; i++)
{
// create a new Goal Panelat the goalIntroParent Position
GameObject goal = Instantiate(goalPrefab, goalIntroParent.transform.position, Quaternion.identity, goalIntroParent.transform);
}
}
Answer by Namey5 · Jul 17, 2020 at 07:21 AM
There is a typo at line 28 and the loop never starts;
//this
for(int i = 0; i > levelGoals.Length; i++)
//should be this
for(int i = 0; i < levelGoals.Length; i++)
Thank you! I always get the greater than and less than symbols mixed up. it did the trick!