Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 GrimFunko · Feb 07, 2018 at 09:46 AM · errorinstantiatenullreferenceexception

Simultaneous Null Reference Exception and expected value

So I've been messing around with instantiating a prefab and then calling the components for edit to try and make save data system that creates when something is saved, and destroys when something is deleted via a toggled selection. When the game is in play mode, every frame returns both a Null Reference Exception and a true or false depending on the condition of the toggle. I'm fairly new to coding and Unity but I haven't experienced this before and I can't seem to fix it.. any and all help would be much appreciated!! Just for some reference, the Value and NewInstant functions are running off a single button, and the deleteToggled on another button aims to check the condition of the toggles and destroy instances accordingly.`using System.Collections; using System.Collections.Generic; using UnityEngine;
using UnityEngine.UI; using System;

public class Buttons : MonoBehaviour {

 public GameObject Entry;
 private int value = 0;

 private GameObject new1;
 private GameObject new2;
 private GameObject new3;
 private GameObject new4;

 public Transform Panel;

 public Toggle Toggle0;
 public Toggle Toggle1;
 public Toggle Toggle2;
 public Toggle Toggle3;
 public Toggle Toggle4;


 public Text fab1;
 public Text fab2;
 public Text fab3;
 public Text fab4;
 public Text fab5;



 void Start ()
 {
 }
 
 public void Value ()
 { 
     value += 1;
     Debug.Log(value);
 }


 
 public void NewInstant ()
 {    
     if (value < 5) {
         if (new1 == null) {
             new1 = Instantiate (Entry);
             new1.transform.parent = Panel;
             Toggle1 = new1.transform.GetChild(2).GetComponent<Toggle>();
             fab1 = new1.transform.GetChild(0).GetComponent<Text>();
             fab1.text = "This is prefab number " + value.ToString();
             
         } else if (new1 != null) {
             new2 = Instantiate (Entry);
             new2.transform.parent = Panel;
             Toggle2 = new2.transform.GetChild(2).GetComponent<Toggle>();
             fab2 = new2.transform.GetChild(0).GetComponent<Text>();
             fab2.text = "This is prefab number " + value.ToString();

         } else if (new2 != null) {
             new3 = Instantiate (Entry);
             new3.transform.parent = Panel;
             Toggle3 = new3.transform.GetChild(2).GetComponent<Toggle>();
             fab3 = new3.transform.GetChild(0).GetComponent<Text>();
             fab3.text = "This is prefab number " + value.ToString();

         } else if (new3 != null) {
             new4 = Instantiate (Entry);
             new4.transform.parent = Panel;
             Toggle4 = new4.transform.GetChild(2).GetComponent<Toggle>();
             fab4 = new4.transform.GetChild(0).GetComponent<Text>();
             fab4.text = "This is prefab number " + value.ToString();

         }
     }        
 }



 void Update ()
 {
     if (Toggle1.isOn) {
         Debug.Log (1);
     } else { 
         Debug.Log ("not 1");
     }
 }

 public void deleteToggled ()
 {

     if (Toggle1.isOn == true) {
         DestroyObject (new1);
     }
     if (Toggle2.isOn == true) {
         DestroyObject (new2);
     }
     if (Toggle3.isOn == true) {
         DestroyObject (new3);
     }
     if (Toggle4.isOn == true) {
         DestroyObject (new4);
     }

 }

} `

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ShadyProductions · Feb 07, 2018 at 09:55 AM

Hi,

For example your toggle2 is part of a child of gameobject new2

 Toggle2 = new2.transform.GetChild(2).GetComponent<Toggle>();

When you destroy new2 it will also destroy its children which will make toggle2 null.

This goes for all your toggles.

Comment
Add comment · Show 2 · 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 GrimFunko · Feb 07, 2018 at 10:31 AM 0
Share

Hello! This is true, it makes sense before the instance has been created i.e at the start and straight after destroying, but after creating a new instance the Toggles are/should be reassigned to the new instance's children (I think), so when all 4 (or even just the first one) are active there should be no null; in the console at least.

avatar image ShadyProductions GrimFunko · Feb 07, 2018 at 10:56 AM 0
Share

You have to assign the new instance of the toggle back to the original reference of Toggle1 otherwise it will remain null.

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

116 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[Closed]NullReferenceException on Object Instantiation onto game world 2 Answers

Instantiating class creating copies with same instance ID of 0 0 Answers

NullReferenceException and m_InstanceID == 0 on LoadLevel (C#) 1 Answer

After Instantiating a Prefab I get a NullReference Error even though the Object was created 2 Answers

Error putting an object into an Array 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