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 VoxPlacitum · Apr 29, 2014 at 02:38 PM · colornullreferenceexceptiongetcomponent

NullReferenceException....Again(sorry)

I have scoured the internet and answers to help get this figured out but I'm still stuck. I'm just starting out and feel bad, as this question seems to have been answered many times. ANYWAY...

I'm trying to have my level be spawned, in relation to the player(they will be "digging" downward). I want each layer to have its own color and that color to be created randomly. So I've tried to create an array in the worldBuild script to save the values "on the fly" and the probability script to to take those values and feed it to the renderer to change the color(the probability code is on the Cube prefab that I'm instantiating in worldBuild). So here are the questions

1- Why am I getting the NullReferenceException on line 34 of probability?(I thought that I referenced the component(script) correctly...)

2- The code is not changing color for anything but the renderer is there.(checked by renderer.material.color = color.cyan;)

3- As a noobie to coding in general and even more so c#, am I approaching this incorrectly?

Any at all help would be greatly appreciated. Thanks folks.

Code here (an incredible mess from trying to fix it so many times)

 public class worldBuild : MonoBehaviour {
 
     public Transform Cube;
     public Transform Player;
     public int layer;
     public float[,] layerList;
 
         
 
         
         
         public float[,] getLayerListCol()
         {
             layerList[(Mathf.Abs(layer)), 0] = Random.value;
             layerList[(Mathf.Abs(layer)), 1] = Random.value;
             layerList[(Mathf.Abs(layer)), 2] = Random.value;
             layerList[(Mathf.Abs(layer)), 3] = 1.0f;
             return layerList;
         }
 
         public float[,] updateLayerList()
         {
             layerList = new float[50, 4];
             return layerList;
         }
     
         public void make()
         {
             for (layer = 0; layer > Player.transform.position.y - 10; layer--)
             {
                 for (int x = 0; x < 50; x++) 
                 {
                     Instantiate(Cube, new Vector3(x, layer, 0), Quaternion.identity);
                 }
             }
         }
 
         
 
     void Start() 
     {
         Instantiate(Player, new Vector3(25, 1, 0), Quaternion.identity);
         updateLayerList ();
     }
     
     
     // Update is called once per frame
     void Update ()
     {
         if (layer > (Player.transform.position.y - 10))
             make ();
             
             getLayerListCol ();
     }
 
 }


 public class probability : MonoBehaviour 
 {
     public Transform Cube;
     public GameObject theNittyGritty;
     private Color changingColor;
 
     private worldBuild colorList;
     public float colR;
     public float colG;
     public float colB;
     public float colA;
     public int yVal;
     public float[,] layerList;
 
 
     // Use this for initialization
     void Start ()
     {
 
 
         int yVal = (int) Cube.transform.position.y;
 
         int odds = Random.Range (1, 100);
 
         if(odds < Mathf.Abs(yVal))
             Destroy(gameObject);
 
         colorList = theNittyGritty.GetComponent<worldBuild> ();
         layerList = colorList.layerList;
 
         colR = layerList[Mathf.Abs (yVal), 0];
         colG = layerList[Mathf.Abs (yVal), 1];
         colB = layerList[Mathf.Abs (yVal), 2];
         colA = layerList[Mathf.Abs (yVal), 3];
 
         changingColor = new Color(colR, colG, colB, colA);
 
          renderer.material.color = changingColor;
          
 
 
     }
     
     // Update is called once per frame
     void Update () 
     {
 
     }
 
     void OnDestroy() 
     {
         DestroyImmediate (renderer.material);
     }
 }
Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

20 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

Related Questions

NullReference Problem with pragma strict 1 Answer

NullReferenceException when using GetComponent to access script variables 1 Answer

DLL NullReferenceException 1 Answer

script GetComponent, nullReference error 1 Answer

Has my project's data corrupted? Script keeps returning null but 20 minutes ago it wasn't? 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