Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Mar 21, 2017 at 09:55 PM by hexagonius for the following reason:

https://docs.unity3d.com/Manual/NullReferenceException.html

avatar image
0
Question by gguttilla · Mar 21, 2017 at 06:01 AM · shadernullreferenceexceptionmaterialsarraysforeach

Null Reference Exception when changing material shader

I am trying to create a script that makes all the materials of all objects obstructing the view of the player become transparent, and become opaque when they are no longer obstructing the view. It seems to work fine in the editor now, but whenever I go from having >= 1 object blocking the view to having no objects blocking the view I am flooded with null reference exception errors that point specifically to mats = rends.materials; near the bottom. Through debugging i've found that the error seems to stem from specifically referencing the materials of gameobjects in the if (hits.length == 0) { statement. Any thoughts?

using UnityEngine; using System.Collections;

public class ObstructionFade : MonoBehaviour {

 public Camera cam;
 public Renderer[] rend;
 public Renderer[] lastRend;
 public RaycastHit[] hits;
 public Material[] mats;
 public int i;
 LayerMask mask;

 void Start () {
     mask = LayerMask.GetMask ("Default");
 }

 // Update is called once per frame
 void LateUpdate () {



     i = 0;
     Ray camRay = cam.ScreenPointToRay (cam.WorldToScreenPoint (transform.position + new Vector3 (0, 2, 0)));
     Debug.DrawLine (cam.transform.position, transform.position);




     //gets all things the raycast hits
     hits = Physics.RaycastAll (camRay, Vector3.Distance (Camera.main.transform.position, transform.position) - 3, mask);




     //for every thing the raycast hits
     foreach (RaycastHit hit in hits){

         //set last renderer hit to the current renderer
         lastRend[i] = rend[i];

         //get the current renderer hit and make it transparent
         rend[i] = hit.collider.gameObject.GetComponent<MeshRenderer> ();
         mats = rend [i].materials;
         foreach (Material mat in mats) {
             mat.color = new Color (mat.color.r, mat.color.g, mat.color.b, 0.3f);
             mat.shader = Shader.Find ("Legacy Shaders/Transparent/Diffuse");
         }

         //if the last and current renderer dont match make the last renderer opaque
         if (lastRend[i] != rend[i]){
             mats = lastRend [i].materials;
             foreach (Material mat in mats) {
                 mat.color = new Color (mat.color.r, mat.color.g, mat.color.b, 1.0f);
                 mat.shader = Shader.Find ("Standard");
             }

         }
         //increment i
         i++;
     }



     //if the raycast hits nothing make all previously hit objects opaque
     if (hits.Length == 0) {
         foreach (Renderer rends in rend) {
             mats = rends.materials;
             foreach (Material mat in mats) {
                 mat.color = new Color (mat.color.r, mat.color.g, mat.color.b, 1.0f);
                 mat.shader = Shader.Find ("Standard");
             }
         }
     }
 }

}

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

  • Sort: 

Follow this Question

Answers Answers and Comments

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

Shader displaying with weird colors 1 Answer

Unable to edit grey ,,locked,, Materials 3 Answers

How to only access one object in array if it's index is 1 0 Answers

2 materials 1 sprite,2 materials for 1 sprite 0 Answers

Effects that I made in a HDRP project don't work in a URP project. 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