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 beau101023 · May 22, 2015 at 01:06 AM · errorterrainobjectreferencegenerator

Object reference not set to an instance of an object.

Upon starting my game, I receive this error in the console:

 NullReferenceException: Object reference not set to an instance of an object
 Terrain_Generator.Start () (at Assets/Terrain_Generator.cs:28)

It says the prevRow object reference is not set even though it should be set in the code below:

 using UnityEngine;
 using System.Collections;
 
 
 public class Terrain_Generator : MonoBehaviour 
 {
     public GameObject Target_Obj;
     public int width;
     public int length;
     public int height;
     int prevPos;
     int horizPos;
     GameObject prevRow;
     int prevRowPos;
     int prevHigh;
     int prevLow;
     void Start () 
     {
         for (int j = 0; j < width + 1; j = j + 1) 
         {
             for (int i = 0; i < length + 1; i = i + 1) 
             {
                 prevRowPos = j - 1;
                 horizPos = Random.Range (prevPos - height, prevPos + height + 1);
                 if(j >= 1)
                 {
                     prevRow = GameObject.Find ("Cube (" + i + "," + prevRowPos + ")");
                     if(horizPos > prevRow.transform.position.y)
                     {
                         horizPos = (int)prevRow.transform.position.y + 1;
                     }
                     if(horizPos < prevRow.transform.position.y)
                     {
                         horizPos = (int)prevRow.transform.position.y - 1;
                     }
                 }
                 Object.Instantiate (Target_Obj, new Vector3 (j, horizPos, i), Quaternion.identity);
                 prevPos = horizPos;
             }
         }
     }
 }


Comment
Add comment · Show 2
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 fafase · May 22, 2015 at 03:45 AM 0
Share

If it is created from Instantiate and a prefab then it is named with a (Clone) extension.

avatar image fafase · May 22, 2015 at 03:52 AM 0
Share

Well then, as long as the rest of the code gets assumed, we can only assume a solution. Posting all the relevant code would probably fix this in a $$anonymous$$ute.

2 Replies

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

Answer by LessThanEpic · May 22, 2015 at 01:13 AM

GameObject.Find can return null. Have you tried printing out what the result of

 "Cube (" + i + "," + prevRowPos + ")"

is and verified that you have an active game object of that name when that code runs?

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 beau101023 · May 22, 2015 at 01:25 AM 0
Share

I have verified that an active game object with this name does exist.

Active Object: Cube (0,0)

Attempted reference: Cube (0,0)

Still returning null.

Am currently using Unity version 4.5.4f1, if that information is useful.

avatar image LessThanEpic · May 22, 2015 at 01:55 AM 0
Share

When does your cube get created? You're trying to find it in the Start method and if it's being created by another script then the other script may not have had a chance to run yet.

avatar image beau101023 · May 22, 2015 at 03:33 AM 0
Share

The cube is created in this script, the Target_Obj object is referencing the cube prefab, which has a script that automatically names it once it is created based on it's x and z positions.

Perhaps it hasn't had a chance to name it... I might try sticking a delay in the code somewhere.

avatar image LessThanEpic · May 22, 2015 at 03:53 AM 1
Share

Yeah, the script on the new game object probably hasn't had a chance to run yet. You could get rid of the other script and rename the game object in this script. Something like:

     GameObject newCube = (GameObject) Object.Instantiate (Target_Obj, new Vector3 (j, horizPos, i), Quaternion.identity);
     newCube.name = //whatever you want to name it goes here

avatar image beau101023 · May 22, 2015 at 07:08 PM 0
Share

That might just be the ticket, I've been wondering how to reference an object that was just instantiated. Will test shortly.

Show more comments
avatar image
1

Answer by p3k07 · May 22, 2015 at 05:17 AM

try: Object metaTarget = Instantiate(Target_Obj, new Vector3 (j, horizPos, i), Quaternion.identity);

or GameObject instead of Object.

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

21 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

Related Questions

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

NullReferenceException: Object reference not set to an instance of an object 0 Answers

Script decides not to run anymore 1 Answer

Object reference not set to... 1 Answer

Do you have to set a object reference for every script? 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