Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by H2O · Dec 19, 2014 at 02:46 AM · uiaccess

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ashleyjlive · Dec 19, 2014 at 03:19 AM 0
Share

I tried this script and no error was presented to me.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ashleyjlive · Dec 19, 2014 at 03:26 AM 0
Share

I would assume it's the toggle element from Unity 4.6 UI

avatar image H2O · Dec 19, 2014 at 10:03 AM 0
Share

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;
     }
 }
avatar image H2O · Dec 19, 2014 at 10:09 AM 0
Share

And yes its a UI toggle from 4.6

avatar image Kiwasi · Dec 19, 2014 at 12:06 PM 0
Share

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.

avatar image H2O · Dec 19, 2014 at 07:15 PM 0
Share

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!

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges