Can't assign camerarig as child of instantiated players,Can't parent camerarig object to instantiated players.
I am new to Unity and coding in general, I did the TANKS! tutorial from 2015, and made a copy of the game with custom models, but I want to implement a split-screen camera instead of the top-down two-player setup. I have already set up the two cameras and they are following and looking at two empty objects. I want to assign these empty game objects as children of the player tanks as they spawn in. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class TankParentToCaemraRig : MonoBehaviour {
public GameObject CameraRig1;
public GameObject CameraRig2;
// Start is called before the first frame update
void Start()
{
if (GameObject.Find("GameManager").GetComponent<TankManager>().m_PlayerNumber == 1)
{
CameraRig1.transform.parent = this.transform;
}
if (GameObject.Find("GameManager").GetComponent<TankManager>().m_PlayerNumber == 2)
{
CameraRig2.transform.parent = this.transform;
}
}
// Update is called once per frame
void Update()
{
}
}
Above is the code I came up with to achieve this, but it's not working and showing me a null exception error. Earlier it was showing this error - ArgumentException: GetComponent requires that the requested component 'GameObject' derives from Mono. The code used to instantiate the tank prefab is using System; using UnityEngine;
[Serializable] public class TankManager { public Color m_PlayerColor;
public Transform m_SpawnPoint;
[HideInInspector] public int m_PlayerNumber;
[HideInInspector] public string m_ColoredPlayerText; [HideInInspector] public GameObject m_Instance;
[HideInInspector] public int m_Wins;
private TankMovement m_Movement;
private TankShooting m_Shooting;
private GameObject m_CanvasGameObject;
public void Setup()
{
m_Movement = m_Instance.GetComponent<TankMovement>();
m_Shooting = m_Instance.GetComponent<TankShooting>();
m_CanvasGameObject = m_Instance.GetComponentInChildren<Canvas>().gameObject;
m_Movement.m_PlayerNumber = m_PlayerNumber;
m_Shooting.m_PlayerNumber = m_PlayerNumber;
m_ColoredPlayerText = "<color=#" + ColorUtility.ToHtmlStringRGB(m_PlayerColor) + ">PLAYER " + m_PlayerNumber + "</color>";
MeshRenderer[] renderers = m_Instance.GetComponentsInChildren<MeshRenderer>();
for (int i = 0; i < renderers.Length; i++)
{
renderers[i].material.color = m_PlayerColor;
}
}
public void DisableControl()
{
m_Movement.enabled = false;
m_Shooting.enabled = false;
m_CanvasGameObject.SetActive(false);
}
public void EnableControl()
{
m_Movement.enabled = true;
m_Shooting.enabled = true;
m_CanvasGameObject.SetActive(true);
}
public void Reset()
{
m_Instance.transform.position = m_SpawnPoint.position;
m_Instance.transform.rotation = m_SpawnPoint.rotation;
m_Instance.SetActive(false);
m_Instance.SetActive(true);
}
}
,I am new to Unity and coding in general, I did the TANKS! tutorial from 2015, and made a copy of the game with custom models, but I want to implement a split-screen camera instead of the top-down two-player setup. I have already set up the two cameras and they are following and looking at two empty objects. I want to assign these empty game objects as children of the player tanks as they spawn in. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class TankParentToCaemraRig : MonoBehaviour {
public GameObject CameraRig1;
public GameObject CameraRig2;
// Start is called before the first frame update
void Start()
{
if (GameObject.Find("GameManager").GetComponent<TankManager>().m_PlayerNumber == 1)
{
CameraRig1.transform.parent = this.transform;
}
if (GameObject.Find("GameManager").GetComponent<TankManager>().m_PlayerNumber == 2)
{
CameraRig2.transform.parent = this.transform;
}
}
// Update is called once per frame
void Update()
{
}
}
Above is the code I came up with to achieve this, but it's not working and showing me a null exception error. Earlier it was showing this error - ArgumentException: GetComponent requires that the requested component 'GameObject' derives from Mono. The code used to instantiate the tank prefab is using System; using UnityEngine;
[Serializable] public class TankManager { public Color m_PlayerColor;
public Transform m_SpawnPoint;
[HideInInspector] public int m_PlayerNumber;
[HideInInspector] public string m_ColoredPlayerText; [HideInInspector] public GameObject m_Instance;
[HideInInspector] public int m_Wins;
private TankMovement m_Movement;
private TankShooting m_Shooting;
private GameObject m_CanvasGameObject;
public void Setup()
{
m_Movement = m_Instance.GetComponent<TankMovement>();
m_Shooting = m_Instance.GetComponent<TankShooting>();
m_CanvasGameObject = m_Instance.GetComponentInChildren<Canvas>().gameObject;
m_Movement.m_PlayerNumber = m_PlayerNumber;
m_Shooting.m_PlayerNumber = m_PlayerNumber;
m_ColoredPlayerText = "<color=#" + ColorUtility.ToHtmlStringRGB(m_PlayerColor) + ">PLAYER " + m_PlayerNumber + "</color>";
MeshRenderer[] renderers = m_Instance.GetComponentsInChildren<MeshRenderer>();
for (int i = 0; i < renderers.Length; i++)
{
renderers[i].material.color = m_PlayerColor;
}
}
public void DisableControl()
{
m_Movement.enabled = false;
m_Shooting.enabled = false;
m_CanvasGameObject.SetActive(false);
}
public void EnableControl()
{
m_Movement.enabled = true;
m_Shooting.enabled = true;
m_CanvasGameObject.SetActive(true);
}
public void Reset()
{
m_Instance.transform.position = m_SpawnPoint.position;
m_Instance.transform.rotation = m_SpawnPoint.rotation;
m_Instance.SetActive(false);
m_Instance.SetActive(true);
}
}