- Home /
How do I assign a Gameobject's reference to a field variable as a parameter in a function?
I am trying to create an Event Trigger for a GameObject that involves updating a specified field from another GameObject. For example, I have a GameObject named GameData with a public int field named modeStyle and another public int named initialHealth. I then want an event trigger to call an outside function that takes either one of these int fields as a parameter to update the GameData. Is there a specific way for this to be done? I have yet to determine the proper way to code this.
Sort of like this, even though it's wrong:
public void UpdateGameData(GameData.mode as int)
{
Debug.Log("Mode Style:" + mode);
}
Answer by RendrWyre · Jan 11, 2020 at 06:23 PM
Edited in response to your comment.
public class GameData : Monobehaviour
{
public int gameScoreA;
public int gameScoreB;
public void UpdateGameScore(int gameScoreToUpdate int newScore)
{
if (gameScoreToUpdate == 1)
{
gameScoreA = newScore;
}
if (gameScoreToUpdate == 2)
{
gameScoreB = newScore;
}
}
}
public class SomeScript : MonoBehaviour
{
public GameObject gameDataGO;
private void Start()
{
int newScoreToAdd = 100;
gameDataGO.GetComponent<GameData>().UpdateGameScore(1, newScoreToAdd);
}
}
Your terminology is a little confusing and so is your architecture.
"I have a GameObject named GameData" I'm going to assume you mean you have a script called GameData attached to a GameObject. Hopefully I understood what you are trying to do.
Ok... so there's a few ways you can go about doing this.
1) use a static class or method.
namespace MyGame.Managers
{
public static class MyStaticClass
{
public static int gameScore { get; private set; )
public static void UpdateGameScore(int newScore)
{
gameScore = newScore;
}
}
}
This would allow you to call the method UpdateGameScore() without actually having a reference to it.
using MyGame.Managers;
namespace MyGame.Player
{
public class PlayerManager : MonoBehaviour
{
public void Start()
{
int newScore = 50;
MyStaticClass.UpdateGameScore(newScore);
}
}
}
2) use some kind of reference
namespace MyGame.Managers
{
public class GameManager : MonoBehaviour
{
public int gameScore { get; set; }
}
}
namespace MyGame.Player
{
public class Player : Monobehaviour
{
public GameObject gameManager;
public void Start()
{
gameManager.GetComponent<GameManager>().gameScore = 100;
}
}
}
3) Reference the script and invoke a method.
namespace MyGame.Managers
{
public class GameManager : MonoBehaviour
{
public int gameScore { get; private set; }
public void UpdateGameScore(int pointsToAdd)
{
gameScore += pointsToAdd;
}
}
}
namespace MyGame.Player
{
public class Player : Monobehaviour
{
public GameObject gameManager;
public void Start()
{
int pointsToAdd = 50;
gameManager.GetComponent<GameManager>().UpdateGameScore(pointsToAdd);
}
}
}
Ok. I do have a script called GameData that acts as a component for the GameObject named "Game Data", so I am indeed referring to the script. Apologies.
Now looking at your first example for reference:
namespace $$anonymous$$yGame.$$anonymous$$anagers
{
public static class $$anonymous$$yStaticClass
{
public static int gameScore { get; private set; )
public static void UpdateGameScore(int newScore)
{
gameScore = newScore;
}
}
}
What if I had two different gameScore ints such as "gameScoreA" and "gameScoreB", and I want the UpdateGameScore function to select one of these two ints to add the newScore into? How would I make a parameter that chooses either of these ints? That's basically the question I was asking, if I haven't been clear enough.
Thanks. That does seem like the most logical approach. Although that does seem to complicate the process for me by being forced to add more various and specific conditions into the one function. $$anonymous$$y main objective was to reduce the amount of lines needed to select different fields in order to guarantee better time complexity, and I figured the referencing of these values into a parameter would be beneficial to that objective. If that is the best I can do however, based on Unity's fickle serialization system, I will have to abide by the current answer.
Before I accept the answer as correct, do you have anything else to add, considering my goal for time complexity?
I partially figured it out. It can be done by adding ref to the function.
namespace $$anonymous$$yGame.$$anonymous$$anagers
{
public static class $$anonymous$$yStaticClass
{
public static int gameScoreA { get; private set; )
public static int gameScoreB { get; private set; )
public static void UpdateGameScore(ref int gameScore, int newScore)
{
gameScore = newScore;
}
}
}
But now I have a few new problems with Unity's response to the function. First, when not static, the Event Trigger I am trying to add refuses to reveal the function within the game object the component is linked toward. Perhaps Unity does not allow serialization of the ref argument. Second, when the entire class is static, it does not allow $$anonymous$$onoBehavior for some reason, so I cannot assign the class to a game object. I tried having the Event Trigger search for the script ins$$anonymous$$d, but that only has access to $$anonymous$$onoScript, which does not include the function. Any ideas on how to get Unity to respond to my particular function?
@jbabineau You can not assign a static class to a gameobject because that's the function of a static class. A static class can not be instanced (and can not inherit from $$anonymous$$onobehaviour) .... adding a script to a gameobject means you are creating an instance of that class at runtime. You should not need to add ref to it. You should be able to call a static function from anywhere. It might be easier using the non-static version at the top of my answer.
Your answer
Follow this Question
Related Questions
Getting log information from event trigger component on the game object 0 Answers
Passing a rectangle as a parameter seems to have no effect on original using StartCoRoutine 1 Answer
NullReferenceException: Object reference not set to an instance of an object 0 Answers
Object Reference not set to an Instance 2 Answers
Object reference not set to an instance of an object. 1 Answer