Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 sergiobd · Jun 26, 2015 at 05:17 AM · classesglobal variable

Best way of passing a parameter to a class

Hi, I have particle class which needs some parameters from my main behaviour class. These parameters should be changed in the editor in real time. I am wondering what is the best way of passing them to the class.

I've thought of 4 possible ways: 1. Declaring them as static 2. Including them as an attribute of each particle (not a good idea) 3. Passing a parent class to each particle and accesing the global parameters from the parent class 4. Passing them as arguments to the function that needs them

The first method would not let me update the variable in the inspector. The second one doesn't make much sense. I think the 3rd method makes sense, but I haven't seen this kind of things in Unity. The fourth is ok, but I should pass a lot of parameters, so I wonder if there is a better option...

 using UnityEngine;
 using System.Collections;
 
 public class ParticleScript : MonoBehaviour {
 
     // A series of global parameters
     public float p1 = 1f;
     public float p2 = 2f;
     // They can be a lot of parameters actually...
 
     // *1* I could make them static, but I dont really 
     // need them to be accesible from anywhere else than the Particle class
     
     void Update () {
         Particle p = new Particle ();
         p.MethodThatNeedsParameters ();
     }
 }
 
 public class Particle{
 
     // *2* Including them as attributes in each particles makes no conceptual sense.
     // And I should use an CustomEditor if I want to update them
 
     // *3* I could pass the parent class as a parameter like
     // ParticleScript parent;
     // And then pick the parameters from there (I am not sure if they would be updated)
 
     public void MethodThatNeedsParameters(){
     // *4* Passing them as arguments is an option, but they are many
 
     // Do something with the parameters
     }
 
 
 }








Comment
Add comment · Show 3
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 Vice_Versa · Jun 26, 2015 at 05:28 AM 0
Share

not sure if particles transforms, but if they do you can do particle.transform.SetParent(parent game object name.transform); something along those lines

avatar image _Yash_ · Jun 26, 2015 at 06:16 AM 0
Share

use static reference of ParticleScript in particle class and assign it once. it will allow you to access global var of ParticleScript and you dont have to pass reference of class too, also vars are visible in inspector.

avatar image sergiobd · Jun 26, 2015 at 03:54 PM 0
Share

@Vice_Versa, as it is a custom particle class, it does not have a transform.

1 Reply

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

Answer by fafase · Jun 26, 2015 at 06:36 AM

My advice on it.

First off, I see one wrong consideration in your code:

  // *1* I could make them static, but I dont really 
  // need them to be accesible from anywhere else

Static does not mean access, you could actually make private static and then no access outside the class.

But now let's try.

You claim to have a lot of data to pass. Instead of having each data (p1, p2,...) as member of ParticleScript, I would create a new class in which those would be:

 public class ParticleScript:monoBehaviour{
      public DataStorage data;
 }
 [System.Serializable]   // This is important to display class in inspector
 public class DataStorage{
     public float p1, p2;
 }

Now you can pass your data object:

 void Update () {
      Particle p = new Particle ();
      p.MethodThatNeedsParameters (data);
  }
 
  public class Particle{
 
      public void MethodThatNeedsParameters(DataStorage data){
            Debug.Log(data.p1);
            Debug.Log(data.p2);
      }
  }
Comment
Add comment · Show 2 · 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 sergiobd · Jun 26, 2015 at 03:57 PM 0
Share

That is a very good advice @fafase. What about passing a ParticleScript reference to the Particle class? Are there any drawbacks about this approach?

avatar image fafase · Jun 26, 2015 at 04:24 PM 0
Share

nope, same.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Caching global vareiables? (n00b question) 1 Answer

[SOLVED] Global Variable in C# class seems to be acting like a local variable. 1 Answer

class design preference 1 Answer

Inheritance from object to object 1 Answer

Where can I find Unity Script Tutorials that teach how to make the scripts come together? 2 Answers


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