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 Avash · Oct 25, 2014 at 12:29 PM · c#error

Object reference not set to an instance of an object in functions

I have a function in other script, but when I call it from other script and give a GameObject as parameter it gives error.

 public void Generate (Vector3 hit){
         this.transform.position = hit;
         Vector3 position = this.transform.position;
         position.x = Mathf.Round(position.x / snap) * snap;
         position.y = Mathf.Round(position.y / snap) * snap;
         position.z = Mathf.Round(position.z / snap) * snap;
         this.transform.position = position;
         Vector3 gridStart = transform.position - new Vector3(worldX, worldY, worldZ);
         for(float x =0F; x<worldX; x += snap) {
             for(float y =0F; y<worldY; y += snap) {
                 for(float z =0F; z<worldZ; z += snap) {
                     GameObject gridCube = Instantiate(prefab) as GameObject;
                     Vector3 startPosition = transform.position + new Vector3(worldX, worldY, worldZ);
                     gridCube.transform.position = new Vector3(gridStart.x + x, gridStart.y, gridStart.z + z);
                     gridCubesList.Add(gridCube);
                 }
             }
         }
         IsGenerated = true;
     }

 public Vector3 snapPoint (GameObject GroundObject){
         Debug.Log(GroundObject);
         List<GameObject> freeCubesList = new List<GameObject>();
         GameObject[] freeCubes = null;
         List<GameObject> GroundObjectCubesList = new List<GameObject>();
         GameObject[] GroundObjectCubes;
         List<GameObject> possibleCubesList = new List<GameObject>();
         GameObject[] possibleCubes = null;
         List<Vector3> localPositionsList = new List<Vector3>();
         Vector3[] localPositions = null;
         List<Vector3> globalPositionsList = new List<Vector3>();
         Vector3[] globalPositions;
         List<float> distanceList = new List<float>();
         float[] distances = null;
         int minIndex; // Shortest distance
         GroundObject.transform.position = this.transform.position;
         foreach (GameObject gridCube in gridCubesList){
             gridTrigger trigger = (gridTrigger) gridCube.GetComponent(typeof(gridTrigger));
             if(trigger.isTriggered(GroundObject)){
                 localPositionsList.Add(this.transform.position - gridCube.transform.position);
                 localPositions = localPositionsList.ToArray();
                 GroundObjectCubesList.Add(gridCube);
                 GroundObjectCubes = GroundObjectCubesList.ToArray();
             }
             if(trigger.isTriggered(null) != true){
                 freeCubesList.Add(gridCube);
                 freeCubes = freeCubesList.ToArray();
             }
         }
 
         foreach (GameObject current in freeCubes){
             foreach(Vector3 localPosition in localPositions){
                 foreach (GameObject freeCube in freeCubes){
                     Vector3 globalPosition = freeCube.transform.position + localPosition;
                     globalPositionsList.Add (globalPosition);
                     globalPositions = globalPositionsList.ToArray();
                     if(globalPosition == freeCube.transform.position){
                         possibleCubesList.Add(freeCube);
                         possibleCubes = possibleCubesList.ToArray();
                     }
                 }
             }
         }
         foreach (GameObject possibleCube in possibleCubes){
             distanceList.Add(Vector3.Distance(possibleCube.transform.position, transform.position));
             distances = distanceList.ToArray();
         }
         minIndex = Array.IndexOf(distances, distances.Min());
         return possibleCubes[minIndex].transform.position;
     }

And in other script:

 GroundObject.rigidbody.MovePosition(grid.snapPoint(GroundObject) + speed * Time.deltaTime);

EDIT: So how do I share a GameObject with an istance with other script?

Comment
Add comment · Show 9
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 Landern · Oct 25, 2014 at 12:31 PM 1
Share

@Avash, please add your code, it's temping to close this since this question is probably one of the most asked, least searched for on UA.

avatar image Landern · Oct 25, 2014 at 01:08 PM 0
Share

@Avash, how is the grid object in the below being instantiated, this is actually the most important part and the part where you left out the detail. Is this a variable that is assigned in the inspector? Are you using GetComponent() to try and get a reference.

 GroundObject.rigidbody.$$anonymous$$ovePosition(grid.snapPoint(GroundObject) + speed * Time.deltaTime);

Have you tried adding debugging code to ensure your objects have reference.

 if(GroundObject)
   Debug.Log("GroundObject isn't null");
 else
   Debug.Log("GroundObject is null, i should fix this");
 
 if(grid)
   Debug.Log("grid isn't null, niiiice!");
 else
   Debug.Log("grid is null, i should fix this because it creates exceptions");

Also, why wouldn't you give actual error that points to the line of code that the exception is thrown from?

Edit: There a lot of assumptions of existing structure in the snapPoint method, this is why the location of the error is paramount to helping you.

avatar image Avash · Oct 25, 2014 at 03:10 PM 0
Share

It says error is in line 70.

avatar image Landern · Oct 25, 2014 at 03:12 PM 1
Share

@Avash, (sigh) obviously there isn't a line 70 in your code that is in your original post, please indicate which line is line 70.

avatar image Avash · Oct 25, 2014 at 03:37 PM 0
Share

GroundObject isn't null.

Show more comments

1 Reply

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

Answer by NoseKills · Oct 25, 2014 at 08:09 PM

You say line 19 throws the error and that's:

 if(trigger.isTriggered(GroundObject)){
 

The only thing there that can throw a null ref error is if trigger is null

Either your gridCube in that loop didn't have that component or... I'm not sure if that line where you try to get the component is right

 gridTrigger trigger = (gridTrigger) gridCube.GetComponent(typeof(gridTrigger));
 

What if you try

 gridTrigger trigger = gridCube.GetComponent<gridTrigger>();


Comment
Add comment · Show 6 · 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 Avash · Oct 26, 2014 at 10:18 AM 0
Share

No, it doesn't help.

avatar image NoseKills · Oct 26, 2014 at 10:48 AM 0
Share

That means then that at least one gridCube in gridCubesList does not have a component gridTrigger attached to it.

You'll just have to find out why that is or post all the code where gridCubesList is populated etc...

Also you could add some Debug.Log into the loop to see if any of the gridCube have that component or is it missing from all of them.

  foreach (GameObject gridCube in gridCubesList)
  {
           gridTrigger trigger = (gridTrigger) gridCube.GetComponent(typeof(gridTrigger));
           if (trigger != null) Debug.Log("Trigger found");

...

avatar image Avash · Oct 26, 2014 at 12:15 PM 0
Share

Trigger found and an error.

avatar image Avash · Oct 26, 2014 at 12:22 PM 0
Share

I posted generating function.

avatar image NoseKills · Oct 26, 2014 at 11:16 PM 1
Share

How many times do you get "Trigger Found" and how many entries are in gridCubesList ?

If you double click the error in Unity console, which line does it take you to in $$anonymous$$onoDevelop?

If line 19 truly was if(trigger.isTriggered(GroundObject)){ (in the original code sample) and you said that's where the error comes from, there's no other choice than trigger being null because a function (`isTriggered`) can't be null and passing a null parameter (`GroundObject`) is totally O$$anonymous$$.

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

SetActive() Object reference not set to an instance of an object. 1 Answer

Array index out of range error 1 Answer

Semicolons are seen as an unexpected symbol 1 Answer

Getting index of the smallest number in a list 1 Answer

A fast triangle triangle intersection algorithm for unity? 4 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