- Home /
Can't access Instantiated UI element
Hi, I've looked all over but I can't find a similar problem anywhere!
I'm trying to instantiate a toggle prefab, and make it a child of a canvas and access its postition etc.
Here is my code:
private void SpawnToggle(GameObject[] array){
GameObject canvas = GameObject.Find("Canvas");
for(int i = 0; i < array.Length; i++){
GameObject toggle = Instantiate(togglePrefab) as GameObject;
toggle.transform.parent = canvas.transform;
}
}
The problem is, this line - "toggle.transform.parent = canvas.transform;" Gives me a NullReferenceException.
A toggle will spawn, then it was error. If I remove that line, all my toggles spawn fine. But I cant access them.
Am I missing something really obvious?
I've attached the prefab, and everything works until I try and access toggle.
Any Ideas??
Thanks!
I tried this script and no error was presented to me.
Answer by Kiwasi · Dec 19, 2014 at 03:22 AM
What is the type of togglePrefab? A safe cast (as) returns null if the source object cannot be cast to the destination object. Use this line to throw a more meaningful error.
GameObject toggle = (GameObject)Instantiate(togglePrefab);
I would assume it's the toggle element from Unity 4.6 UI
Hi, Thanks for replying!
I tried that and got this exception: InvalidCastException: Cannot cast from source type to destination type.
I'm really not sure what to do as I wrote it just like turorials, and as Ashleyjlive said, it worked for him! Could it be a bug?
Here is the full code if it help:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ToggleSpawn : $$anonymous$$onoBehaviour {
private GameObject[] catArray;
public Transform togglePrefab;
void Start () {
catArray = GetCategories();
SpawnToggle(catArray);
}
void Update () {
}
private void SpawnToggle(GameObject[] array){
GameObject canvas = GameObject.Find("Canvas");
for(int i = 0; i < array.Length; i++){
GameObject toggle = (GameObject)Instantiate(togglePrefab);
toggle.transform.parent = canvas.transform;
}
}
private GameObject[] GetCategories()
{
GameObject[] array = GetComponent<FindAllCategoriesAvailable>().GetCatArray();
return array;
}
}
The type of togglePrefab Transform. You can't cast a transform to a GameObject. Two options to fix.
Change line 8 to:
public GameObject togglePrefab;
Or line 28 to
GameObject toggle = ((Transform)Instantiate(togglePrefab)).gameObject;
The first is more efficient, and should be used unless you have a reason not too.
thank you! this - GameObject toggle = ((Transform)Instantiate(togglePrefab)).gameObject;
worked. The top one didn't however.
I don't really understand, but I'm very grateful!
Thank you again!
Answer by orihq · Oct 18, 2016 at 05:14 AM
Instantiating UI elements is alays tricky for me. So I started to use gameObject.SetActive (true); or false.
Your answer

Follow this Question
Related Questions
Adding 3D objects to the new UI System 2 Answers
Access a JavaScript variable from a C# script? 2 Answers
How to animate RectTransform change in position 1 Answer
Clicks are detected through the UI elements 2 Answers
Access is denied in Windows 8 App 0 Answers