Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Creating render textures and materials at runtime

Discussion in 'Scripting' started by Joweh, Dec 5, 2021.

  1. Joweh

    Joweh

    Joined:
    Aug 22, 2021
    Posts:
    4
    I'm having an issue with creating render textures at runtime, here is what i'm trying to do:

    I'm trying to create a system where you add cameras & display planes to a placeholder object and then the script will create the render textures, assign them to the cameras, create a material from the render texture and assign that to one of the display planes.

    I can't even get one to create at the moment, here is the error:

    Any guidance appreciated.



    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class CCTVCameraOperation : MonoBehaviour
    7. {
    8.     [SerializeField] GameObject[] CCTVScreens;
    9.     [SerializeField] Camera[] cameras;
    10.     private RenderTexture[] renderTextures;
    11.     private Material[] renderMaterials;
    12.     // [SerializeField] float changeInterval = 5.0f;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         print("Number of Cameras: " + cameras.Length);
    18.         print("Number of CCTV Screens: " + CCTVScreens.Length);
    19.  
    20.         for (int i = 0; i < cameras.Length; i++)
    21.         {
    22.             print("loop pass i : " + i);
    23.             // Create Render Texture
    24.             renderTextures[i] = new RenderTexture(256, 256, 16, RenderTextureFormat.ARGB32);
    25.             renderTextures[i].Create();
    26.             renderTextures[i].name = "Render Texture " + i;
    27.  
    28.             renderMaterials[i].mainTexture = renderTextures[i];
    29.             renderTextures[i] = RenderTexture.active;
    30.             cameras[i].GetComponentInChildren<Camera>().targetTexture = renderTextures[i];
    31.             CCTVScreens[i].GetComponentInChildren<Renderer>().material.mainTexture = renderTextures[i];
    32.         }
    33.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,710
    Fortunately it does not even matter what you're trying to do when you get a NullReference. It just doesn't.

    The answer is always the same... ALWAYS. It is the single most common error ever.

    Don't waste your life spinning around and round on this error. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception
    - also known as: Object reference not set to an instance of an object

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    You need to figure out HOW that variable is supposed to get its initial value. There are many ways in Unity. In order of likelihood, it might be ONE of the following:

    - drag it in using the inspector
    - code inside this script initializes it
    - some OTHER external code initializes it
    - ? something else?

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.

    Here is a clean analogy of the actual underlying problem of a null reference exception:

    https://forum.unity.com/threads/nul...n-instance-of-an-object.1108865/#post-7137032
     
  3. Joweh

    Joweh

    Joined:
    Aug 22, 2021
    Posts:
    4
    Thanks for the reply, however, I don't understand what I need to do to make them not null (I'm a Unity novice). My planes and cameras should get values from when I drag them into the properties part of the script but i'm creating the render textures and materials from scratch so I don't know what I need to set them as by default.

    I've been wracking my brain over this for week and have been through the manual trying various methods but none have worked yet.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,710
    Let me state it another way:

    Until you do step 1 above, everything else is wasted time.

    Step 1: Identify what is null.

    So I ask you, What is null? I can't tell that from here. Only you can tell that from the information in the console window, and the steps outlined in the link above.
     
  5. Joweh

    Joweh

    Joined:
    Aug 22, 2021
    Posts:
    4
    I can see that the render textures array is the null exception, I've put a check in to see if it's null and output a statement to the log (as suggested in your post) but it doesn't log it because it throws the error when trying to access it to check.
     
  6. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    374
    So initialize the array before adding to it (in the start function), something like:

    renderMaterials = new Material[camers.Length];
     
    Kurt-Dekker likes this.
  7. Joweh

    Joweh

    Joined:
    Aug 22, 2021
    Posts:
    4
    That did it, thanks. I had tried that before within the loop I think and it didn't work.