Problems with script var sharing
I'm making a game where the player can change color to be able to walk on or go though colored blocks. Below are the two script I have created to do the task. The PlayerColorChecker.cs go on the block and the PlayersCurrentColor.cs goes on the player script. In the code there are debuging line where im checking to see if var that im am trying to share is show, but in the space where the value should be it is blank why is this, also the script is on the RigidBodyFPSController from the sample assets if this changes anything. Thank you.
https://gist.github.com/awk888/013b6116dc411be6ea7082969082bc8b
Answer by KevRev · Apr 17, 2019 at 08:12 PM
Your first script is referencing the Player GameObject, but you need to reference the component on the GameObject.
Add the following to you variable declarations:
public PlayersCurrentColor playerColor;
Start()
{
playerColor=Player.GetComponent<PlayersCurrentColor>();
}
Then when you want to reference the colour, use the following:
Debug.Log("color: " + playerColor.PlayerColor);
If you want to reference it directly, you can instead do the following:
Debug.Log("color: " + Player.GetComponent<PlayersCurrentColor>().PlayerColor);
Although it's better practise to grab it in a variable first.
Ok good luck. If it helps, it'd be great if you could mark this as the answer. I'm new here and trying to build my rep :)
https://gist.github.com/awk888/956a820971745f2ab59e8cc454fe0876
Is this the way you said to right it?
Yes, but move the GetComponent out of the Start function and make it the first line in your CheckPlayerColor function.
Does this work? Are there any errors in the console?