• 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 Araconda · Mar 05, 2014 at 11:56 AM · gameobjecttransformrenderer.enabled

Access renderer from transform

Hi guys,

I have been trying to access the Renderer from a Transform that I have instantiated. The error is "Object reference not set to an instance of an object", and it appeared at the GridEnable and GridDisable functions, where I called the tile.gameObject.renderer.enabled.

T$$anonymous$$s is the GridScript, w$$anonymous$$ch control the grid in a 2D plane:

 using UnityEngine;
 using System.Collections;
 
 public class GridScript : MonoBehaviour {
     
     public int gridWidth = 9;
     public int gridHeight = 9;
 
     public int remainingGrid;
     public int totalGrid;
     
     public Transform[,] grid;
     public int[,] gridStatus;
     public int[,] gridBorder;
     
     private PlayerControl playerControl;
     
     // Use t$$anonymous$$s for initialization
     void Awake ()
     {
         playerControl = GameObject.FindWithTag ("Player").GetComponent<PlayerControl>();
     }
     
     public void GridEnable(Transform tile)
     {
         tile.gameObject.renderer.enabled = true;
         int x = Mathf.RoundToInt (tile.localPosition.x);
         int y = Mathf.RoundToInt (tile.localPosition.y);
         gridStatus [x, y] = 0;
     }
 
     public void GridDisable(Transform tile)
     {
         tile.gameObject.renderer.enabled = false;
         int x = Mathf.RoundToInt (tile.localPosition.x);
         int y = Mathf.RoundToInt (tile.localPosition.y);
         gridStatus [x, y] = 1;
     }
     }

And T$$anonymous$$s is the GridSetup, w$$anonymous$$ch used to generate the grid from a text file

 using UnityEngine;
 using System.Collections;
 
 public class GridSetup : MonoBehaviour {
 
     public int gridHeight = 9;
     public int gridWidth = 9;
     public Transform normalTile;
     public TextAsset textFile;
 
     private GridScript gridScript;
     private string text;
 
     // Use t$$anonymous$$s for initialization
     void Awake () {
         gridScript = GetComponent<GridScript>();
         text = textFile.text;
         CreateGrid (text, gridScript);
     }
 
     // Translate a text file into a map of grid, store in gridScript
     void CreateGrid (string text, GridScript gridScript) {
 
         // Initialize the grid
         gridScript.gridHeight = gridHeight;
         gridScript.gridWidth = gridWidth;
         gridScript.grid = new Transform[gridWidth + 2, gridHeight + 2];
         gridScript.gridStatus = new int[gridWidth + 2, gridHeight + 2];
         gridScript.gridBorder = new int[gridWidth + 2, gridHeight + 2];
         gridScript.totalGrid = gridHeight * gridWidth;
 
         int xx = 0;
         int yy = gridHeight;
         foreach (char c in text) 
         {
             if (c == '\r') continue;
             if (c == '\n') {
                 xx = 0;
                 yy--;
             }
             else {
                 xx++;
                 gridScript.grid[xx, yy] = 
                     Instantiate(normalTile, new Vector3((float)xx, (float)yy, 8f), Quaternion.identity) as Transform;
                 if (c == 'x') {
                     gridScript.GridDisable(gridScript.grid[xx, yy]);
                     gridScript.totalGrid--;
                 }
                 else
                 {
                     gridScript.GridEnable(gridScript.grid[xx, yy]);
                 }
             }
         }
 
         gridScript.remainingGrid = gridScript.totalGrid;
     }
 }
 
Comment
Add comment · Show 4
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 Calum-McManus · Mar 05, 2014 at 12:59 PM 0
Share
avatar image Araconda · Mar 05, 2014 at 01:09 PM 0
Share
avatar image Calum-McManus · Mar 05, 2014 at 01:19 PM 0
Share
avatar image Araconda · Mar 05, 2014 at 01:27 PM 0
Share

1 Reply

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

Answer by Calum-McManus · Mar 05, 2014 at 01:31 PM

You are setting up a load of Transforms in your scene that form a grid, not$$anonymous$$ng in these scripts are visible. you would have to Instantiate gameobjects to form the grid not transforms.

Comment
Add comment · Show 3 · 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 Araconda · Mar 05, 2014 at 02:51 PM 0
Share
avatar image Calum-McManus Araconda · Mar 05, 2014 at 02:56 PM 0
Share
avatar image perchik · Mar 05, 2014 at 02:56 PM 0
Share

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.

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

22 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 avatar image

Related Questions

How do I reference the transform component of a gameObject to a script automatically? 0 Answers

Transform not changing to correct target transform 0 Answers

change the front point or head of a GameObject 1 Answer

Need workaround for this line of code 1 Answer

Adding a prefab to gameObject at a certain position in runtime 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