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 /
This question was closed Sep 14, 2011 at 07:53 PM by yeoldesnake 1 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by yeoldesnake 1 · Sep 14, 2011 at 06:46 PM · exceptionmessagebroadcastmessagenullreferencebroadcast

BroadcastMessage Null Reference Exception Problem

I have this piece of code here which is a part of an administrative console i am making. I tried to give the user a bit more freedom as to what type the BroadcastMessage Parameters are.

 consoleInterpretation=consoleText.Split("."[0]);
 if(consoleInterpretation[0]=="Find"){
 if(consoleInterpretation.length==4){
 var typeSplit2:String[];
 typeSplit2=consoleInterpretation[3].Split(":"[0]);
 if(typeSplit2.length==2){
 
 if(typeSplit2[1]=="int")
 GameObject.Find(consoleInterpretation[2]).BroadcastMessage(consoleInterpretation[2],int.Parse(typeSplit2[0]));
 if(typeSplit2[1]=="float")
 GameObject.Find(consoleInterpretation[2]).BroadcastMessage(consoleInterpretation[2],float.Parse(typeSplit2[0]));
 if(typeSplit2[1]=="String")
 GameObject.Find(consoleInterpretation[2]).BroadcastMessage(consoleInterpretation[2],typeSplit2[0]);
 if(typeSplit2[1]=="GameObject")
 GameObject.Find(consoleInterpretation[2]).BroadcastMessage(consoleInterpretation[2],GameObject.Find(typeSplit2[0]));
 else
 GameObject.Find(consoleInterpretation[2]).BroadcastMessage(consoleInterpretation[2],typeSplit2[0]);
     }
     else
 GameObject.Find(consoleInterpretation[2]).BroadcastMessage(consoleInterpretation[2],consoleInterpretation[3]);
 }
 else 
 GameObject.Find(consoleInterpretation[1]).BroadcastMessage(consoleInterpretation[2]);
 }

When i type "Find.MyGameObject.AddPoints.8:int" i get the following error

 NullReferenceException
 UnityEngine.GameObject.BroadcastMessage (System.String methodName, System.Object parameter) (at C:/BuildAgent/work/842f9557127e852/Runtime/ExportGenerated/Editor/UnityEngineGameObject.cs:287)

A Debug.Log line i added showed me that typeSplit2[0]="8" and typeSplit2[1]="int" Any ideas as to what might be the problem?

EDIT: Now even if i type Find.MyGameObject.SampleFunction.text Where: SampleFunction(test:String) It gives a null reference exception

Comment
Add comment · Show 1
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 DaveA · Sep 14, 2011 at 07:24 PM 0
Share

You might want to investigate the 'switch' statement for general program$$anonymous$$g coolness

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by DaveA · Sep 14, 2011 at 07:27 PM

You assume that whatever they type and ends up in consoleInterpretation[2] is going to be valid. I wouldn't. Do a Find on that, check the result for null (didn't find), and put up an error message if so. Then use that var you got back instead of consoleInterpretation[2] all over the place. In fact, check all user input for validity before trying to broadcast it.

Re 'typeSplit2[0]="8" and typeSplit2[1]="int" ': that's right. Split on "." gives you

 [0] Find
 [1] MyGameObject
 [2] Addpoints
 [3] 8:int

[3] split on ":" gives

 [0] 8
 [1] int


"Find.MyGameObject.SampleFunction.text Where: SampleFunction(test:String)" split on "." gives:

 [0] Find
 [1] MyGameObject
 [2] SampleFunction
 [3] text Where: SampleFunction(test:String)

and [3] split on ":" gives

 [0] text Where
 [1]  SampelFunction(test
 [2] String)
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 DaveA · Sep 14, 2011 at 07:36 PM 0
Share

Edited to illustrate

avatar image yeoldesnake 1 · Sep 14, 2011 at 07:39 PM 0
Share

Checking back at my code i see that i am trying to find the gameobject by consoleInterpretation[2] which is the function's name. Would totally have missed it , thanks. Funny how its always these small damn things.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

BroadcastMessage Functionality Help 1 Answer

Send messages to different gameobjects 1 Answer

OnClickUpAsButton used externaly 0 Answers

Are message broadcasts instant? 1 Answer

StackOverflowException on UI Button message 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