- Home /
Question by
PhoenixBlackReal · May 13, 2014 at 09:34 PM ·
classes
Access a script and all of its functions and values on the same gameObject from another script.
There are two gameObjects in the game: Player1 and Player2. They both share a script InputReader and a script DoSomething.
public class InputReader : MonoBehaviour{
//properties
public GameObject yourObj;
public bool inputIsReceived;
public bool hasScriptAttached;
public bool willReceiveJoystick1Input;
public bool willReceiveJoystick2Input;
/*public GetInput(){
inputIsReceived = false;
}*/
public void CheckIfScriptIsAssigned(){
//Checks the object name and assigns proper Input
//Player1
if(gameObject.name == "Player1"){willReceiveJoystick1Input = true;
Debug.Log("Will receive Joystick 1 Input");
}
//Player2
if(gameObject.name == "Player2"){willReceiveJoystick2Input = true;
Debug.Log("Will receive Joystick 2 Input");
}
}
public void CheckInput (){
if(willReceiveJoystick1Input){
if(Input.GetKeyDown(KeyCode.A)){
Debug.Log ("Input player1 received");
}
}
if(willReceiveJoystick2Input){
if(Input.GetKeyDown(KeyCode.B)){
Debug.Log ("Input player2 received");
}
}
}
//public aGetInput x = new aGetInput();
void Start (){
//CheckIfScriptIsAssigned();
}
void Update (){
//CheckInput();
}
}
What do I write in DoSomething script, so I could use any function from InputReader script at any time? Like x.CheckInput() in update and x.CheckIfScriptIsAssigned() in start. Really at a loss here ._.
Comment
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Have two scripts modify eachother 1 Answer
How can I reference/import one C# script from another? 2 Answers
Where is the best place to host a class that is not intended to be a MonoBehaviour or Component? 1 Answer
Where can I find Unity Script Tutorials that teach how to make the scripts come together? 2 Answers