Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jbabineau · Jan 11, 2020 at 05:27 PM · referenceparametersevent triggering

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);
     }



Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
         }
     }
 }


Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jbabineau · Jan 11, 2020 at 06:39 PM 0
Share

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.

avatar image RendrWyre jbabineau · Jan 11, 2020 at 06:59 PM 0
Share

@jbabineau I updated my original answer.

avatar image jbabineau RendrWyre · Jan 11, 2020 at 07:17 PM 0
Share

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?

avatar image jbabineau · Jan 11, 2020 at 07:07 PM 0
Share

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?

avatar image RendrWyre jbabineau · Jan 11, 2020 at 07:13 PM 0
Share

@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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

121 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges