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 Jure · Jan 11, 2012 at 10:37 AM · instantiatearraygame object

Array with game object: object reference not set to an instance of an object

I have a script which genrates 2d grid out of cubes. Before I made it with 2 for's and tile = Instantiate (tilePrefab, Vector3(j, i, 0), Quaternion.identity);

I wanted to easier control those tiles so I made array:

 var tileList : GameObject[,];
 tileList = new GameObject[y,x];
 tileList[j,i] = Instantiate (tilePrefab, Vector3(j, i, 0), Quaternion.identity);

affter that I check in some other array what type is that tile so that I would assing a color to it. It worked fine before but not I get: object reference not set to an instance of an object.

Code is like this:

 if(tileGrid[i,j]<15)
   {
       tileList[i,j].material.color = Color.green;
       }...

This is in same for loop like that code before. Before I started storing tiles in array it wored perfectly.

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 syclamoth · Jan 11, 2012 at 10:45 AM 0
Share

Could you be more specific about exactly where the for-loop is? By the looks of things, you're rebuilding the entire array every iteration! Could you tell us where the brackets are (since I think that's the important detail, here)?

avatar image Jure · Jan 11, 2012 at 04:43 PM 0
Share

Ok I will post the whole code but it's quite some of it I think.

var tilePrefab : GameObject;

var y : int = 5; var x : int = 5;

var initialColor : Color;

var tileGrid : int[,]; var tileList : GameObject[,];

 function Start (){

tileGrid = new int[y, x]; tileList = new GameObject[y,x]; rand$$anonymous$$ap();

for (var i : int = 0;i < y; i++) { for (var j : int = 0; j < x; j++) { tileList[j,i] = Instantiate (tilePrefab, Vector3(j, i, 0), Quaternion.identity);

   if(tileGrid[i,j]<15)
   {
       tileList[i,j].material.color = Color.green;
       }
   else if(tileGrid[i,j]<18)
   {
       tileList[i,j].renderer.material.color = Color.blue;
       }
   else if(tileGrid[i,j]<20)
   {
       tileList[i,j].renderer.material.color = Color.gray;
       }
   else if(tileGrid[i,j]<22){
       tileList[i,j].renderer.material.color = Color.cyan;
       }
   else
   {
       tileList[i,j].renderer.material.color = Color.yellow;
   }
       
       
   
   if (i == y/2 && j == x/2)
   {
       //print("Found middle of the map");
     cameraPosition= tileList[i,j].transform.position; // get the tile position...
     cameraPosition.z = Camera.main.transform.position.z; // but keep the camera z 
     Camera.main.transform.position = cameraPosition; // move camera to the position
     tileList[i,j].renderer.material.color = Color.magenta;

   }
 }

} }

function OnGUI () { var e : Event = Event.current; if (e.is$$anonymous$$ouse){ if($$anonymous$$eyCode.$$anonymous$$ouse2 ){ mousePositon = Vector3(Input.mousePosition.x,Input.mousePosition.y,Input.mousePosition.z); cameraPosition = Vector3(Input.mousePosition.x,Input.mousePosition.y,-25.0); Camera.main.transform.position = cameraPosition; Debug.Log("$$anonymous$$ouse position is:"+mousePositon); Debug.Log("Camera position is:"+cameraPosition); }; };

1 Reply

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

Answer by Jure · Jan 11, 2012 at 09:28 PM

I got it answerd on IRC chat. I had to change code in IF statment to somthing like this: tileList[i,j].renderer.material.color = Color.green;

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Missing (Game Object) 3 Answers

Make object move in a direction depending on where it spawns? (C#) 1 Answer

Updating a component on a NetworkView game object doesn't update the component on other clients 1 Answer

Different array value at different times 1 Answer

Prefabs instantiated from an array are keeping their public int value 1 Answer


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