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 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.

This is the GridScript, which 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 this 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 This is the GridSetup, which 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 this 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

Can you try and format your scripts properly so it's easier to read. Also can you share the whole script, as from this people can't tell if you are even calling the function at all.

avatar image Araconda · Mar 05, 2014 at 01:09 PM 0
Share

Sorry for the bad format. I have posted the two scripts. Hope it can help :)

avatar image Calum-McManus · Mar 05, 2014 at 01:19 PM 0
Share

The error your getting is because you are trying to access a "GameObject" from your tile transform and there isn't one, transform are not gameobjects and only gameobjects have renderers

avatar image Araconda · Mar 05, 2014 at 01:27 PM 0
Share

So is there any other ways rather than create an array of GameObjects ins$$anonymous$$d of Transform?

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, nothing 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

$$anonymous$$any thanks to Calum.$$anonymous$$acmanus, I have fixed my code. The problem is because I instantiate Transform ins$$anonymous$$d of Gameobject.

The

 gridScript.grid[xx, yy] = 
               Instantiate(normalTile, new Vector3((float)xx, (float)yy, 8f), Quaternion.identity) as Transform;

should be changed to

 gridScript.grid[xx, yy] = 
               (Instantiate(normalTile, new Vector3((float)xx, (float)yy, 8f), Quaternion.identity) as GameObject).transform;
avatar image Calum-McManus Araconda · Mar 05, 2014 at 02:56 PM 0
Share

If you accept my answer it will close the question, glad I could help :)

avatar image perchik · Mar 05, 2014 at 02:56 PM 0
Share

If Calum solved your problem, please mark his answer as the solution

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

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

Adding a prefab to gameObject at a certain position in runtime 0 Answers

Creating new Transform from existing objects Transform to Instantiate object 1 Answer

Animation issue (OnTriggerEnter) ***SOLVED*** 1 Answer

move a GameObject based on Input.GetTouch 1 Answer

Scale (Transform.localscale) a gameobject based on ARFaceAnchor anchorData 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