What am i overlooking? -GameObject.GetComponent()-
Okay so essentially I am trying to make a Door/Debris disappear when I press the E key and have enough points. Here is my code(s)
public int Points;
// Use this for initialization
void Start ()
{
Points = 500;
}
public void AddPoints(int PointsToAdd)
{
Points += PointsToAdd;
}
public void MinusPoints(int PointsToMinus)
{
if (Points - PointsToMinus < 0)
{ // }
else { Points -= PointsToMinus; }
}
I have attached that to the item Cam and it has no compiler error but when I use this.....
public GameObject Cam;
public GameObject Buytext;
public GameObject Barrier;
public GameObject SorryText;
// Use this for initialization
void Start ()
{
Buytext.SetActive(false);
}
void OnTriggerEnter()
{
Buytext.SetActive(true);
}
void OnTriggerStay()
{
if (Input.GetKeyDown(KeyCode.E))
{
Barrier.SetActive(false);
Buytext.SetActive(false);
}
}
void OnTriggerExit()
{
Buytext.SetActive(false);
}
Earlier I had tried to call upon my points variable in the if statement under OnTriggerStay like this...
if (Input.GetKeyDown(KeyCode.E & Cam.GetComponent<PlayerPoints>.Points = 500) {
Barrier.SetActive(false);
Buytext.SetActive(false);
}
but it gave me the error :
CS0119 'GameObject.GetComponent()' is a method, which is not valid in the given context
What am I doing wrong or am overlooking???
Answer by smnerat · Aug 20, 2016 at 09:28 PM
You were close, but you have a few formatting issues.
When you call a method you need to use ().
If you use a operator in an if statement, it needs to be doubled: == or &&.
You missed a closing parenthesis after the Keycode.E.
if (Input.GetKeyDown(KeyCode.E) && Cam.GetComponent<PlayerPoints>().Points == 500) { Barrier.SetActive(false); Buytext.SetActive(false); }
Your answer
Follow this Question
Related Questions
Javascript GetComponent not work! 0 Answers
Disable function in another script 1 Answer
Best way to reach a component or script's function? 1 Answer
Semicolon expected before end of line 0 Answers