This is the code of the class I want to get variable Speed from (attached with Player Object)
public class PlayerManager : MonoBehaviour
{
public float Speed;
void Update()
{
InvokeRepeating ("RaiseSpeed", 1.0f, 1.5f);
}
void RaiseSpeed() { Speed += Speed/10; }
}
And this is the code of the class in which I want to use Speed
public class ScoreManager :MonoBehaviour
{
public Text TextScore;
public PlayerManager _PlayerManager;
private int Score;
void Start()
{
Score = 0;
_PlayerManager = GameObject.Find("Player").GetComponent<PlayerManager>;
//Which is why cause error
}
void LateUpdate()
{
InvokeRepeating("UpdateScoreText", 0.0f, 1.0f);
}
void UpdateScoreText()
{
Score += _PlayerManager.Speed /10;
TextScore.text = "Score: " + Score.ToString()
}
}
The Error description: Cannot convert method group 'GetComponent' to non-delegate type 'PlayerManager'. Did you intend to invoke the method?
_PlayerManager = GameObject.Find("Player").GetComponent<GameManager>;
You tried to get GameManager instead of PlayerManager
Try this:
_PlayerManager = GameObject.Find("Player").GetComponent<GameManager> as PlayerManager;
Updated:
_PlayerManager = GameObject.Find("Player").GetComponent("PlayerManager") as PlayerManager;
where PlayerManager is name of component in object where is PlayerManager script
Try replacing
_PlayerManager = GameObject.Find("Player").GetComponent<PlayerManager>;
with
_PlayerManager = GameObject.Find("Player").GetComponent<PlayerManager>();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.