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 grfdhfrgtj · Jul 11, 2013 at 09:01 AM · information

Information exchange between scripts

I'm currently working in a game and got a problem with the information exchange between my scripts. That's what i have:

Script A calculates quite much and attached to a emty GameObject. Script B is needed for the movement behaviors of my monsters. Sripts B is attached to each monster in the scene. Script B needs a public variable of Script A. How can i get this information? Script A has get-methods/properties for this, but i don't know how i can call them? Maybe somehow with get_component?

PS: I don't want script B to inherit script A, because script A should only be running once.

Thanks for your help

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 grfdhfrgtj · Jul 11, 2013 at 12:07 PM 0
Share

i have just linked scriptA as variable in Script B. It seems to work fine:

 GameObjectLoger gameloger= GameObject.FindGameObjectWithTag("scriptmanager").GetComponent<GameObjectLoger>();

When i try to use the gameloger-methods the variables seem to be emty,although they are not.

 public GameObject[]list;
 
 gameloger.Get_Go_list_via_copy(list);
 
 //in GameObjectLoger Sript:
 
 public void  Get_Go_list_via_copy(GameObject[] go)
     {
 for (int i = 0; i < go_list.Length;i++){
             go = new GameObject[go_list.Length];
             go[i] = go_list[i];
         }
     }
 }

What did i wrong?

avatar image moonstruck · Jul 11, 2013 at 12:30 PM 0
Share

Change line 7 to public void Get_Go_list_via_copy(out GameObject[] go) and the line 3 to gameloger.Get_Go_list_via_copy( out list ).

The problem is that the reference on the array list is passed to the function by value so the changes you do inside the function are not affecting the passed object. out keyword allows function to save the result in the variable. Though it is a better idea just to use the return value because it make the code more readable.

Also add to the line 9 (just before the loop) the following: go = new GameObject[ go_list.Length ] to create the array itself.

avatar image fafase · Jul 11, 2013 at 12:51 PM 0
Share

@moonstruck first off, I am not stalking you :) But what you say is wrong. The reference of the array is passed by value but this value points where the array is stored, so you ca modify the original array. What you are doing with using out is that the reference (the address where the array is stored) can now be modified, which is quite dangerous if you do not know what you are doing.

The problem here is that

 for (int i = 0; i < go_list.Length;i++){
      go = new GameObject[go_list.Length];
      go[i] = go_list[i];
 }

Create a new array on each iteration and only store one guy. You probably have only the last one populated.

You need your method as such:

 public void  Get_Go_list_via_copy(GameObject[] go)
 {
     go = new GameObject[go_list.Length];
     for (int i = 0; i < go_list.Length;i++){
          go[i] = go_list[i];
        }
     }
 }

This creates two distinct array pointing to the same objects. If you want a shallow copy you simply point one towards the other:

 public void  Get_Go_list_via_copy(GameObject[] go)
 {
     go = go_list.Length;
 }

Now if you want a deep copy meaning both arrays have their own set of objects. That is go has an independant copy of go_list you need a deep copy which includes copying all members of the object into a new object that is then passed to the array.

avatar image moonstruck · Jul 11, 2013 at 03:41 PM 0
Share

Yeah, you are right, I haven't noticed the 10th line. :)

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by moonstruck · Jul 11, 2013 at 10:30 AM

Using static variables is ok in some cases but it can cause subtle problems in complex programs. Instead you can add a public variable of type ScriptA into your ScriptB and either assign it via the inspector or using any find function (FindObjectsOfType( typeof( ScriptA ) ) as ScriptA, Find( "ScriptAObjectName" ).GetComponent(), etc.) if your ScriptB objects are spawned dynamically. Then you can access any public properties, variables, etc. from your ScriptA using scrptAVariable.someVariable syntax.

Comment
Add comment · Show 1 · 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 grfdhfrgtj · Jul 11, 2013 at 11:02 AM 0
Share

Find( "ScriptAObjectName" ).GetComponent< ScriptA >() thats exactly what i was looking for:) thanks for your help

avatar image
0

Answer by zerebruin · Jul 11, 2013 at 09:06 AM

declare a static variable (or function) in Script A

 public static var someStaticVar

you can call that variable from any other script (like Script B) by calling scriptname.variable so

 ScriptA.someStaticVar
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 grfdhfrgtj · Jul 11, 2013 at 09:37 AM 0
Share

ok, thanks should work now fine

avatar image fafase · Jul 11, 2013 at 12:16 PM 0
Share

This is a real bad advice for someone that is beginning. The other answer by moonstruck should be prefered as long as you do not understand how static and instance work.

Also, this has many answer or even long explanantio like here: http://unitygems.com/script-interaction1/

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

17 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Material doesn't have a color property '_Color' 4 Answers

Getting information from a cfg file 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