I, as a Java programmer, continually want to reference attributes in other Game Objects or scripts (we use all C# in our game). Until now, I have always been able to avoid the retrieving data from some other Game Object besides what is inherent for a GameObject or Transform (i.e. position, etc.) I have done quite a bit of research on this topic on and off and I think I have concluded that referencing data in other scripts is not really possible in Unity, but it just seems that something so simple like that HAS to be possible.
For example, if I have a GameObject “Points” that holds the number of points for a player object, I don’t know how to reference that number of points from a script inside of the player’s GameObject. What I have done instead is tried to move logic around and get by by doing things like using SendMessage(“AddPointsToPlayer1”, 40);
Is there a way to do something like this in one of the player’s scripts?
public GameObject points; – Set this to the Points GameObject in the Unity Editor
I have tried these methods before and I still get errors when trying to reference data from other scripts:
FYI, I am trying to use this method:
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public OtherScript target;
void Update() {
target.foo = 2;
target.DoSomething("Hello");
}
}
Here is my code:
using UnityEngine;
using System.Collections;
public class PointsForGUI : MonoBehaviour {
//private string player1String = "Player One: 1234";
public GUISkin guiSkin;
private const int yIncrement = 30;
private int player1Points = 41;
private int player2Points = 42;
public PointsSystem pointsSystemScript;
void OnGUI() {
// Assign the skin to be the one currently used.
GUI.skin = guiSkin;
int winscore = pointsSystemScript.getPlayerPoints();
drawPlayerPoints(1, winscore);
drawPlayerPoints(2, winscore);
}
void drawPlayerPoints(int playerNum, int winningScore)
{
int yOffset = 10 + (30 * (playerNum - 1));
GUI.Label(new Rect(10, yOffset, 200, 20), "Player " + playerNum + ": " + winningScore,
"PointsDisplayPL" + playerNum);
}
}
As you can see, I am expecting to get an “int” result from calling:
pointsSystemScript.getPlayerPoints();Am I doing something wrong?
Well, actually. Good question. I realize now that since it did not let me attach my script as the public variable, that I assigned the GameObject that that script was attached to instead. Although, effectively has attached the script because it says “Points (PointsSystem)” and it won’t allow me to drag any other GameObject there assumedly because no other object has the PointsSystem script.
I use this technique all the time and it works fine. Are you SURE you are correctly assigning to pointsSystemScript?
How are you doing the assignment? Interactively in the editor with the dialog? On Awake() or Start() with a FindComponent or something similar?
I have assigned the GameObject containing the script (Points) from inside the editor itself, not in an Awake() or Start() function.
Again, just so things are clear, here is the script that makes the call:
using UnityEngine;
using System.Collections;
public class PointsSystem : MonoBehaviour{
private int player1Points = 4141;
public int getPL1Points()
{
return player1Points;
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public int getPlayerPoints()
{
return player1Points;
}
}
And here is the code that is trying to retrieve the int value resulting in the Null Pointer Exception.
using UnityEngine;
using System.Collections;
public class PointsForGUI : MonoBehaviour {
//private string player1String = "Player One: 1234";
public GUISkin guiSkin;
private const int yIncrement = 30;
private int player1Points = 0;
private int player2Points = 0;
public PointsSystem pointsSystemScript;
void OnGUI() {
// Assign the skin to be the one currently used.
GUI.skin = guiSkin;
int playerScore = pointsSystemScript.getPlayerPoints();
drawPlayerPoints(1, playerScore);
drawPlayerPoints(2, playerScore);
}
void drawPlayerPoints(int playerNum, int winningScore)
{
int yOffset = 10 + (30 * (playerNum - 1));
GUI.Label(new Rect(10, yOffset, 200, 20), "Player " + playerNum + ": " + winningScore,
"PointsDisplayPL" + playerNum);
}
}
Just wanted to update this as the weekend has passed. Anyone have any ideas or simple, working examples of calling another script that retrieves some simple value, such as an int value?
Or, maybe I HAVE to assign the script using the Awake() function or something like that and Unity does not allow you to assign gameobjects with scripts in the editor when you need to retrieve a value from a function in another script…