- Home /
Creating a List of render textures for instantiated GameObjects with cameras
Hello all!
I am attempting to create a system in which the player can Instantiate GameObjects with the click of the mouse, and have those GameObjects be added to a List.
This part is functional; however, I also want to create a List of Render Textures for each Camera that resides within the Instantiated GameObjects. I will then have each cameras target texture set to each render texture with the same index value in the List.
(The ultimate goal is to let the player place cameras around, and later be able to access each camera feed from the camera that was placed).
My problem with the code below is that my List of Render Textures are all blank, and each camera in the instantiated GameObjects do not have the render texture as their target texture.
Here is the code, I removed any irrelevant functions:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class placePrefab : MonoBehaviour
{
private GameObject currentObjectToPlace;
//Lists of cameras
[SerializeField]
List<GameObject> camList = new List<GameObject>();
//Lists of render textures
[SerializeField]
List<RenderTexture> renderList = new List<RenderTexture>();
// Update is called once per frame
void Update()
{
if (currentObjectToPlace != null)
{
ReleaseIfClicked();
}
}
void ReleaseIfClicked()
{
RenderTexture camRender;
Camera objectCamera;
if (Input.GetMouseButtonDown(0))
{
objectCamera = currentObjectToPlace.GetComponentInChildren<Camera>();
camRender = new RenderTexture(256, 256, 16, RenderTextureFormat.ARGB32);
camRender.Create();
objectCamera.targetTexture = camRender;
AddToList(camList, currentObjectToPlace);
AddToList(renderList, camRender);
currentObjectToPlace = null;
}
}
void AddToList(List<GameObject> givenList, GameObject givenObject)
{
givenList.Add(givenObject);
}
void AddToList(List<RenderTexture> givenList, RenderTexture givenTexture)
{
givenList.Add(givenTexture);
}
}