• 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 /
This question was closed Sep 29, 2014 at 09:38 AM by Graham-Dunnett for the following reason:

Duplicate Question

avatar image
0
Question by Paddygezus · Sep 29, 2014 at 07:52 AM · nullreferenceexception

NullReferenceException - Object not set to an instance

Hi All,

I'm having some trouble generating a new instance of an object. The code worked fine on Start but when called up in an Update function seems to go awry. I'm only new to c# and Unity so am using this project to learn, would appreciate anyone's insight into why this might be happening.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 public class Mask : MonoBehaviour 
 {
     public GameObject sphere;
     public GameObject selector;
     public bool isSelected = false;
     string[] maskMats = {"BLUE","GREEN","ORANGE","PURPLE","RED","YELLOW"};
     public string color="";
     public List<Mask> Neighbors = new List<Mask> ();
     public bool isMatched = false;
 
     void Start () 
     {
         CreateMask ();
     }
     
 
     public void CreateMask()
     {
         color = maskMats[Random.Range(0, maskMats.Length)];
         SpriteRenderer spriteRenderer = sphere.AddComponent<SpriteRenderer> ();
         spriteRenderer.sprite = Resources.Load<UnityEngine.Sprite> ("Materials/" + color);
         isMatched = false;
     }
 
 

The update function below is in another script:

 void Update () 
         {
             if (isMatched) 
             {
                 for(int i=0;i<masks.Count;i++)
                 {
                     if(masks[i].isMatched)
                     {
                         masks[i].CreateMask();
                         masks[i].transform.position=new Vector3(
                             masks[i].transform.position.x,
                             masks[i].transform.position.y + 6,
                             masks[i].transform.position.z);
                     }
                 }
     
                 isMatched = false;
             }
 

Comment
Comments Locked · 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 Pendrokar · Sep 29, 2014 at 08:19 AM 0
Share

Don't make us blindly look for the null reference. Add the error, as it contains the line at which it occurs.

1 Reply

  • Sort: 
avatar image
1

Answer by bubzy · Sep 29, 2014 at 09:05 AM

"The update function below is in another script:"

er......

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Using GetComponent Within OnTriggerEnter 0 Answers

[C#] Can't get variable from other script. Yields NullReferenceException 1 Answer

Weird NullReferenceException 2 Answers

Arraylist to GameObject[] array issue. 1 Answer

[Roll-a-ball] Changing UI text (NRE) 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