- Home /
Health on photon
So guys i'm getting here an error and i cant get what´s the problem can some one help me please?
these are the errors : Assets\Health.cs(17,48): error CS0411: The type arguments for method 'GameObject.GetComponentInChildren()' cannot be inferred from the usage. Try specifying the type arguments explicitly.
Assets\Health.cs(22,13): error CS0411: The type arguments for method 'Component.GetComponent()' cannot be inferred from the usage. Try specifying the type arguments explicitly.
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class Health : MonoBehaviour
{
public float hitPoints = 100f;
float currentHitPoints;
public Text healthText;
// Start is called before the first frame update
void Start()
{
currentHitPoints = hitPoints;
healthText = GameObject.Find("Canvas").GetComponentInChildren();
}
void Update()
{
if (GetComponent().isMine) { healthText.text = "Health: " + currentHitPoints; }
}
[PunRPC]
public void TakeDamage(float amt)
{
currentHitPoints -= amt;
if (currentHitPoints <= 0)
{
Die();
}
}
void Die()
{
if (GetComponent<PhotonView>().instantiationId == 0)
{
Destroy(gameObject);
}
else
{
if (GetComponent<PhotonView>().isMine)
if (gameObject.tag == "Player")
{
NetworkManager nm = GameObject.FindObjectOfType<NetworkManager>();
nm.standbyCamera.SetActive(true);
nm.respawnTimer = 5f;
}
{
PhotonNetwork.Destroy(gameObject);
}
}
}
}
Answer by KoenigX3 · May 31, 2020 at 09:57 PM
When you use GetComponent, you need to specify the component's type. A single GameObject can have multiple components, so you need to clear things up.
healthText = GameObject.Find("Canvas").GetComponentInChildren<Text>();
// I assume that you are trying to access PhotonView here
GetComponent<PhotonView>().isMine
Side note: don't use GetComponent in Update. Assign it to a variable on Start so you can use that later.
PhotonView photonView;
void Start()
{
photonView = GetComponent<PhotonView>();
}
void Update()
{
if(photonView.isMine)
{
// Your code
}
}
Answer by Razor1994 · May 31, 2020 at 09:54 PM
GetComponentInChildren expects a type to be given. Lets say you want to find the component "BoxCollider".
For that you would do
GameObject.Find("Canvas").GetComponentInChildren<BoxCollider>();
Answer by romanhutsulyak · May 31, 2020 at 10:08 PM
Tnx mate it realy worked i'm leaving here the script it can help later on somebody
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class Health : MonoBehaviour
{
public float hitPoints = 100f;
float currentHitPoints;
public Text healthText;
PhotonView photonView;
// Start is called before the first frame update
void Start()
{
currentHitPoints = hitPoints;
photonView = GetComponent<PhotonView>();
healthText = GameObject.Find("Canvas").GetComponentInChildren<Text>();
}
void Update()
{
if (photonView.isMine)
{
healthText.text = "Health: " + currentHitPoints;
}
}
[PunRPC]
public void TakeDamage(float amt)
{
currentHitPoints -= amt;
if (currentHitPoints <= 0)
{
Die();
}
}
void Die()
{
if (GetComponent<PhotonView>().instantiationId == 0)
{
Destroy(gameObject);
}
else
{
if (GetComponent<PhotonView>().isMine)
if (gameObject.tag == "Player")
{
NetworkManager nm = GameObject.FindObjectOfType<NetworkManager>();
nm.standbyCamera.SetActive(true);
nm.respawnTimer = 5f;
}
{
PhotonNetwork.Destroy(gameObject);
}
}
}
}
Your answer
Follow this Question
Related Questions
Photon Combat script does not work 0 Answers
My Photon health script doesnt work 2 Answers
Photon Instantiate 2 Answers
photon player freak out! 1 Answer
Photon Networking - What function(s) are called when a player (not me) is spawned? 2 Answers