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 Raspilicious · Jul 30, 2012 at 01:05 PM · c#variablefunctionsend

How to correctly send a variable to another function?

I am trying to run a function in another script, outputting a particular variable to that function so that in the other script, it has that variable and can act on it as it needs to.

Here is the code that activates the function in the other script (which works, as it does the Debug.Log correctly):

 setTankPathingScript.TPathingWhenSelected(selectedUnitNode);

And here is the code that is run:

     public void TPathingWhenSelected(TileNode sN) {
         Debug.Log("You have selected a tank!");
         foreach (TileNode selctdInnerN in sN.nodeLinks) {
             if (sN != null) {
                 selctdInnerN.tileTypeMask = TileNode.tileTypeMask.Giant;
                 Debug.Log("neighbour: " + selctdInnerN.name);
             }
             else
                 Debug.LogWarning("error: ",this);
         }
     }
 }

I think it has something to do with the way I'm sending the 'selectedUnitNode' variable over to the other script and function, but I'm not sure.

I can't for the life of me work out why it keeps having the error. Somebody, please help!

Comment
Add comment · Show 5
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 Raspilicious · Jul 30, 2012 at 01:05 PM 1
Share

Bah! Silly editor thing. Editing code to look correct, please wait.

edit 2: Coded adjusted to look proper.

avatar image Bunny83 · Jul 30, 2012 at 01:49 PM 1
Share

I've realigned your lines to match the true code blocks. That's the reason why i always put the opening bracket in a new line ;) It's much easier to match opening and closing brackets.

avatar image Kryptos · Jul 30, 2012 at 01:56 PM 0
Share

Is TileNode a class or a struct?

avatar image Bunny83 · Jul 30, 2012 at 02:05 PM 0
Share

I'm pretty sure it's a class ;) since a TileNode has "links" to other TileNodes. I don't think a TileNode is composed of other TileNodes ;)

avatar image Raspilicious · Jul 30, 2012 at 02:08 PM 0
Share

yep TileNode is a class

1 Reply

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

Answer by MadDave · Jul 30, 2012 at 01:11 PM

The if statement looks wrong. If sN was null you'd get a crash in previous statement accessing sN.nodeLinks. Did you maybe mean

 if(selctdInnerN != null)
Comment
Add comment · Show 13 · 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 Raspilicious · Jul 30, 2012 at 01:22 PM 0
Share

Tried that, and it didn't work.

The 'Object reference not set to an instance of an object' error points me to the line:

 foreach (TileNode selctdInnerN in sN.nodeLinks)

Could it be the other script that's sending the variable across from the other script?

avatar image MadDave · Jul 30, 2012 at 01:43 PM 1
Share

That can only mean that the parameter you are sending ("sN") is null. Can you post the calling code?

avatar image Kryptos · Jul 30, 2012 at 01:51 PM 1
Share

Either sN or sN.nodeLinks is null.

Try this:

 if (sN == null || sN.nodeLinks == null)
 {
     Debug.LogError("!!ERROR 1!!");
     return;
 }
 foreach (TileNode selctdInnerN in sN.nodeLinks)
 {
     if (selctdInnerN  == null)
     {
         Debug.LogError("!!ERROR 2!!");
         continue;
     }
     // rest of your code here
 }
avatar image Bunny83 · Jul 30, 2012 at 01:54 PM 0
Share

There are only two things that could cause this error:

  • either sN is null

  • or sN.nodeLinks is null

So as $$anonymous$$adDave said, check your variable you're passing to the function. If you want to be able to pass null, handle this case correctly inside the function. If it's null you can't access a member since the object doesn't exist.

avatar image Kryptos · Jul 30, 2012 at 02:25 PM 1
Share

GetComponent retrieves an existing component attached to your gameObject. It does not add this component. If the component does not exist then null is returned.

If you need to attach a new component, use AddComponent ins$$anonymous$$d of GetComponent.

Show more comments

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What is this C# code in javascript 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

script with individual function for each gameObject or public script variable to solve inventory system problem 1 Answer

Find variable in another script using a string 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