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 scottballard · Dec 15, 2013 at 02:48 PM · c#arraygetcomponent

GetComponent with arrays C#?

Ok it may seem simple but I need to transfer a 2d array from one c# script (TileGeneration) to the other script (PlayerAI). The TileGeneration script is located on a gameobject called Chunk and the PlayerAI script is located on a gameobject called Player.


 //This is the Script with the array Tile[,] (which is a int array)
 
 public int[,] Tile;
 
 void Start()
 
 {
 
 Tile = new int[20, 20];
 
 }
 
 
 ----------
 
 
 //This is the second script
 
 GameObject Object1 = GameObject.Find("Chunk");
 
 TileGeneration Script1 = Object1.GetComponent<TileGeneration>();
 
 waypoints = Script1.Tile;



Any help will be appreciated, Thank you in advance.

Comment
Add comment · Show 6
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 robertbu · Dec 15, 2013 at 03:20 PM 0
Share

So why is this not working for you? How is waypoints declared?

avatar image aldonaletto · Dec 15, 2013 at 03:24 PM 1
Share

What's the problem? The scripts seem ok. Have you declared waypoints as int[,] in the second script?

avatar image scottballard · Dec 15, 2013 at 04:10 PM 0
Share

yes I did waypoints was declered as, public int[,] waypoints; but I have solved his by not using getcomponent but by making Tile a public static and that had seemed to solved it. Thank you for the help though.

avatar image fafase · Dec 15, 2013 at 04:15 PM 4
Share

And you should not do that. You are using static but looking at your comment, you do not know what it is. Then you are going to start using static all over the place and nothing will work as expected. Give yourself a challenge and a favor, learn how to use GetComponent and learn what static really does.

Static does not mean "Easy to access between script".

avatar image Statement · Dec 21, 2013 at 11:57 PM 2
Share

+All of the thumbs to @fafase. The static keyword look so tempting for beginners to use since it seems to solve a recurring problem (exchange data or messages between two objects). However, this keyword can cause a lot of problems further down the road. It should not be used as a means to send data from one object to another. It can be used to share a common set of data among multiple instances.

I think within the context of what you currently are doing, it could make sense to use static since I presume that even if you had several PlayerAI scripts in the scene, they would all use the same tilemap - correct?

However as @fafase points out, if you are unsure of what static means, you'd do yourself a favor if you figure it out sooner than later. It doesn't mean that you have to rewrite everything and feel hopelessly lost. You can still use static (but be prepared it may bite you softly or very hard). And I think the best way to learn when static is unfriendly is when you actually hit a problem with it. The "danger" then is how you solve that problem, so you don't choose a solution that may involve even more statics. In most cases, using a non-static member is a better approach.

Show more comments

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

19 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

Related Questions

Find GameObjects with a true boolean and put them in an array? 1 Answer

Using getcomponent with an array 2 Answers

Need an advise, how to find all objects with a specific name and a specific script? 2 Answers

Multiple Cars not working 1 Answer

Retrieve an integer from an array in a different script 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