How to store all components of a GameObject in an array?
I want to rewind time in unity so I need to store all the components and it's values from Transform, Rigidbody2D, BoxCollider2D, etc.
I created a GameObject named "TimeManager" that store all GameObjects, but actually I can store all the components for each GameObject, I'm trying to create a Class named "ComponentList" that have only one parameter "public Component[] components" so I can create an array in TimeManager "public ComponentList[] componentList" and create a lot of componentList to store many components, actually this is not working, if I try to save components with "componentList[i].components = gameObjects[0].GetComponents()" it return NullReferenceException.
Here is all the code I have.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TimeManager : MonoBehaviour
{
public GameObject[] gameObjects;
public ComponentList[] componentList;
private void Start()
{
gameObjects = FindObjectsOfType<GameObject>();
componentList = new ComponentList[gameObjects.Length];
for (int i = 0; i < gameObjects.Length; i++)
{
componentList[i].components = gameObjects[i].GetComponents<Component>();
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ComponentList : MonoBehaviour
{
public Component[] components;
}
This is my first time using the forum, I don't know exactly how It works, hope somebody can help me.
Your answer
Follow this Question
Related Questions
Scene change and creative cube game object 1 Answer
Need some guidance for my first game! 0 Answers
Value doesn't change in game when updated. 2 Answers
Destroying game object 0 Answers