Hi guys,
Making my first 2D game and trying to find out how to access
other scripts public variables ( or methods like in my example code)
other.gameObject.GetComponent(PlayerMovement).isGrounded()
or
other.GetComponent(PlayerMovement).isGrounded()
Doesn’t seem to work. Unity recognise my other script PlayerMovement but nut my public method isGrounded () (or any other public method or variables)
My method is declared as
public bool isGrounded(){
return this.grounded;
}
Example in script trying to access other game objects script.
voidOnTriggerEnter2D(Collider2D other) {
if (other.gameObject.name == “Player”) {
while (other.gameObject.GetComponent(PlayerMovement).isGrounded() == false){
Debug.Log (“Loop”);
}
}
}
in most tutorial on this site you can choose between c#, java…
i am newbe
, so i cannot say, wilt the line work or no
while (other.gameObject.GetComponent.isGrounded() == false)
Usually unless you’re using prefabs, you can just set a public class as your variable name. for example, I have a script called “Variables” which holds public variables.
So in the script I can write “public Variables vars;” and drag the gameObject with Variables script onto the new spot created. Then access the variables with vars.variableName
Or you can use this type of code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class StuffAndStuff : MonoBehaviour {
private Variables vars;
private int localVariable;
void Start () {
//Make connection to Variables, Requires you use/create a tag
GameObject v1 = GameObject.FindGameObjectWithTag ("Variables");
vars = v1.GetComponent<Variables> ();
localVariable = vars.otherVariable;
}
Definitely try more googling and tutorials. Answers like this are prevalent 
Well, faster way would be to use this structure :
/*Game.cs*/
public class Game : MonoBehaviour
{
public static Game game;
public int someVar;
void Awake()
{
game = this;
}
}
/*AnotherScript.cs*/
public class AnotherScript : MonoBehaviour
{
void Update()
{
Game.game.someVar +=-*/ .....
}
}