Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by na3ee1 · Mar 04 at 10:05 AM · parent and child

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);
 }

}

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

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

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

UnityEngine.Canvas' does not contain a definition for `FindWithTag' 1 Answer

Find a rigidbody's root parent then find its child by name 1 Answer

Don't understand parent vs child transform in world and localspace 1 Answer

destroy other child objects when the parent counts more than 1 child 1 Answer

Distinguishing multiple colliders on one object 2 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