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 /
  • Help Room /
avatar image
0
Question by hellojbb1234 · Nov 12, 2016 at 02:53 AM · generationalgorithm

How do I get the the same variable from different instances of a script

I may be going around this the wrong way but I am trying to make a genetic algorithm really for fun it probably isn't needed but my fitness is being defined as the distance from an object however I don't know how to pass the variable of each instance of the gameobject to this breeder/natural selection script

Comment
Add comment · Show 4
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 TBruce · Nov 12, 2016 at 03:03 AM 0
Share

Lets say you have two game objects (call them Object1 and Object2) defined in the inspector hierarchy.

Now lets say you have a class (we will call it $$anonymous$$yClass) with a variable called importantValue defined like so

 using UnityEngine;
 using System.Collections;
 
 public class $$anonymous$$yClass : $$anonymous$$onoBehaviour
 {
     public int importantValue;
 
     // Use this for initialization
     void Start ()
     {
     }
 
     // Update is called once per frame
     void Update ()
     {
     }
 }

Now you would add this class to both game objects (Object1 and Object2). Now there are numerous ways to get theses values. In coded you could do this

 int importantValue1 = GameObject.Find("Object1").GetComponent<$$anonymous$$yClass>().importantValue;
 int importantValue2 = GameObject.Find("Object2").GetComponent<$$anonymous$$yClass>().importantValue;

Now this is just one way to get what you want.

avatar image hellojbb1234 TBruce · Nov 12, 2016 at 03:35 PM 0
Share

So in this case the name is important ok thank you

avatar image TBruce hellojbb1234 · Nov 12, 2016 at 06:33 PM 0
Share

I just use the above as an example. importantValue might has well have been`xyz`. It is just a variable name, you did not specify anything or supply any code. What is important is that the variable is defined as public.

avatar image Glurth TBruce · Nov 12, 2016 at 07:24 PM 0
Share

As opposed to using GameObject.Find("ObjectsName") I would recommend that you create a variable in your class to store a reference to the GameObject.

 public GameObject object1;

This field will show up in the inspector, and allow you to drag any gameobject onto it. then use it like so:

 sum+=object1.GetComponent<$$anonymous$$yClassWithImportantVariable >().importantValue;

Or, you can specify that you want only gameobjects with your script on it by simply specifying that class, rather than GameObject

 public $$anonymous$$yClassWithImportantVariable object1;

Accessing the value inside start or update is even easier: e.g.

 sum+=object1.importantValue;

Edit: I should also mention, using any of the above options, you will want to confirm the object actually exists, before trying to use it's variable, or you will get an nullReferenceException e.g.

 if(object1)//confirms object exists
    sum+=object1.importantValue;


0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Best way to generate meshes based on algebraic functions? 0 Answers

How to add programatically animationClips to one animation from Resources folder? 0 Answers

generating hallways using prefabs based on difficulty 1 Answer

How can I create a plane at any given vertex position? 0 Answers

How to generate assets along a vector3 0 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