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 SuperRockmen · Aug 29, 2015 at 02:56 PM · gameobjectvariablearrayspassing

How to pass arrays from one object to another?

The arrays are initialized in the inspector as shown below which is attached to the same object ("A") that the "Pathing" script is attached to.

IMAGE HERE

The script ("Pathing") has a function below:

     public Transform[] getPath(){
         return path;
 }

I call the above function in the following script ("PathFollower") which is attached to another gameObject ("B"):

 void Start () {
         path=other.getPath();
     }

However it says the following 2 errors:

 NullReferenceException: Object reference not set to an instance of an object
 Pathfollower.Start () (at Assets/Pathfollower.cs:16)


How can I get the array from the other gameObject? I would assume that the code will work cos the array for "A" will be initialized at Start and I will be able to pass on the array to "B" to work on. Thanks in advance for any advice!

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

2 Replies

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

Answer by ransomink · Aug 29, 2015 at 05:04 PM

It's hard to tell without posting your entire script for Pathing and PathFollower. Displaying the lines for each error (along with the script) helps us better understand the problem; so I'm assuming the errors in question pertain to the code posted above.

Question: How is the variable other declared. Is it a reference to the Pathing script, a GameObject type, another type: int, float, string, etc.? Without the full code we can't tell what it is.

If you know what GameObject you need to grab the array from, simply declare the variable other as a public reference to the Pathing script and drag-and-drop the GameObject inside the inspector, like so:

PathFollower script

 using UnityEngine;
 using System.Collections;
 
 public class PathFollower : MonoBehaviour {
 
     public Pathing     other;
     public Transform[] path;
 
     void Start () {
         path = other.getPath();
     }
 }

The variable other will appear inside the inspector on the GameObject using the PathFollower script. As previously stated, just drag-and-drop the GameObject using the Pathing script onto the variable other.

I've just tested this myself and it works. Hope this helps!

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 SuperRockmen · Aug 30, 2015 at 07:06 AM 0
Share

Thanks this works! It seems that I need to drag the script onto the variable other.

avatar image
1

Answer by hexagonius · Aug 29, 2015 at 03:07 PM

if you do not use code to grab a reference to Pathing for the variable other, show it in the inspector and assign it the Pathing script from object A.

Comment
Add comment · Show 3 · 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 SuperRockmen · Aug 29, 2015 at 03:30 PM 0
Share

Hi i do use code to grab a reference to pathing. How do i get the reference to the transform array then?

avatar image hexagonius · Aug 29, 2015 at 03:43 PM 0
Share

that part is correct, it's just that other is not assigned the Pathing script. your search and assignment post must be wrong. are you using Gameobject.Find, or something else? when is it called?

avatar image SuperRockmen · Aug 29, 2015 at 04:15 PM 0
Share

I am using this`Pathing other`to get reference to the pathing script. Am I doing it wrong?

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

Public variable from serialized field for prefab 1 Answer

Why UNITY Hates Me By Reading Array's Length! 1 Answer

[SOLVED]How do I spawn GameObject[] in Transform[]? 2 Answers

I need to activate a function in another script from a script on a different game object 0 Answers

Can't convert GameObject to GameObject[] 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