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 Nk.Andrei · Nov 22, 2013 at 08:54 PM · c#errorclassobject-reference-error

Object reference not set to an instance of an object in c# class.

I have made 2 c# classes to use in a path finding algorithm by following a tutorial: a cell class and a grid class. When I access components from the cell class by using the grid class I get the "Object reference not set to an instance of an object error" but I couldn't figure out why.

This is the first script attached to a random game object to use the grid class

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class agentScript : MonoBehaviour {
 
     public Transform ground;
     public Transform enemy;
     ground pathFind;
     
     void Start () 
     {
         pathFind = new ground(transform, enemy.transform, ground.transform);
         pathFind.Start();
     }
     
     
     
     
     void Update () 
     {
         pathFind.Update();
     }
 }



This is the grid script I use to create the actual script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
   
 
     public class ground
     {
         
         Transform theAgent;
         Transform theEnemy;
         Transform theGround;
         
         Vector3 topLeftCorner;
         cellClass theCell;
         cellClass[,] cellArray;
         int a;
         float cellSize;
         int arraySize ;
         int x;
         int z;
         int distance;
         RaycastHit hit;
         Vector3 currentPosition;
         
         public ground(Transform agent, Transform enemy, Transform ground)
         {
             theAgent = agent;
             theEnemy = enemy;
             theGround = ground;
         }
         
         public void Start()
         {
             a = 4;
             arraySize = 9;
             cellSize = theGround.transform.localScale.z / a;///the size of each cell
             cellArray = new cellClass[arraySize,arraySize];////the size of the array
             topLeftCorner = new Vector3(theGround.transform.position.x /2, theGround.transform.position.y, theGround.transform.position.z/2);
             
             for(x = 1; x < cellSize ; x++)
             {
                 for(z = 1;z < cellSize ; z++)
                 {
                     
                     cellArray[x,z] = theCell;////fill the x,z array with cells
                     cellArray[x,z].cellPos = topLeftCorner + new Vector3(x*cellSize,0,z*cellSize);//THIS IS WHERE I GET THE FIRST ERROR????
                     
                 }
             }
         }
         
         public void Update()
         {
             for(x = 1; x<cellSize; x++)
             {
                 for(z = 1; z < cellSize; z++)
                 {
                     if(Physics.Raycast (cellArray[x,z].cellPos, Vector3.up, out hit, distance))//THIS IS WHERE I GET THE SECOND ERROR DUE TO THE cellArray
                     {
                         //print("abba");
                     }
                     Debug.DrawRay (cellArray[x,z].cellPos,new Vector3(0,distance,0), Color.red);
                 }
             }
         
     
     
     
     
     
         }
         
     }
 



I get the errors because I try to access the cellPos variable in the cellClass by using the cellArray[x,z].cellPos

The last script is the cellClass script using UnityEngine; using System.Collections; using System.Collections.Generic;

     public class cellClass //: System.ValueType
 {
 
     Transform theAgent;
     Transform theEnemy;
     public Vector3 cellPos;
     float distance;
     bool obstacle;
     
     public cellClass(Transform agent, Transform enemy, Vector3 pos )
     {
         theAgent = agent;
         theEnemy = enemy;
         cellPos = pos;
         
         
         
     }
     
     
     
 }
     

I need to know what am I doing wrong and i also need to know if my cellClass can extend System.ValueType.

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
Best Answer

Answer by robertbu · Nov 23, 2013 at 01:16 AM

You need to do some research on the difference between accessing something by reference and by value. Classes are referenced 'by reference.' This means that an array of these classes will just be an array of references...all initially null. In order to fill the array, you need new instances of the class for each array reference. Note you a commet that says "//: System.ValueType", but what you've created is not a value type. If this code is translated from Javascript, then you can consider making 'cellClass' a 'struct' rather than a 'class' to solve your problem. Alternately, you could initialize all the positions in the array with instances of 'cellClass' created by the 'new' operator.

On line 46 you have:

 cellArray[x,z] = theCell;

But the 'theCell' is a private variable that you never initialize. So you are assigning an uninitialized/null value to the position in the array. This is why you get an error on the next line.

Comment
Add comment · Show 1 · 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 Nk.Andrei · Nov 23, 2013 at 03:09 PM 0
Share

didn't notice that

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

16 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

Related Questions

Distribute terrain in zones 3 Answers

NullReferenceException yet Debug.Log shows nothing is null? 1 Answer

Object reference not set to an instance of an object 1 Answer

Don't Understand Error 2 Answers

"The class named is not derived from MonoBehaviour or ScriptableObject!", although class is derived from MonoBehaviour 2 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