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 Cripstat · Nov 04, 2015 at 11:20 PM · scripting problemreferencecommunicationclass instance

"Object reference not set to an instance of the object" yet I can see the reference is working in monodevelop

As title says, using this code to access a function in another script

             GameObject castAccess = GameObject.Find("castInt(clone)");
             castInt castScript = castAccess.GetComponent("castInt") as castInt;
             castScript.removeSelf();

it returns the error Object reference not set to an instance, I have no idea which reference is not set to an instance as I have been through everything...

monodevelop autocompletes the castScript.removeSelf(); call so it finds the removeSelf function in the other script on the other class...

the object "castInt(clone)" is an instantiated prefab which has the script castInt attached to it, it's created on button pressDown and then the code here is called on buttonPressUp

on top of all this I have identical code seemingly working identically in another scene that doesn't error!

any insight would be fantastic!

Comment
Add comment · Show 2
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 NoseKills · Nov 05, 2015 at 05:40 PM 1
Share

Autocomplete has nothing to do with finding null reference exceptions. You have written classes with methods and variables in them and autocomplete knows which class has which methods and variables. A null reference happens when you run your code and try to reference a property of something that doesn't exist.

 GameObject go = null;
 Debug.Log(go.name);

You will get autocomplete on "go.name" because the class GameObject will always have that variable available. But this will throw a null ref when you run it because go is null and there's no gameObject that could give you its name.

To (over)simplify, any variable on the left side of a dot (.) is a good place to start looking. You have 2 of those

 castAccess.GetComponent("castInt")
 And
 castScript.removeSelf();


The error you get should show you which line causes the error. Either castAccess is null because GameObject.Find can't find an object by that name or castScript is null because castAccess doesn't have that component.

When you find out which it is, it's easier to find out why. But we can't help u much more without knowing more about the situation

avatar image Cripstat NoseKills · Nov 05, 2015 at 06:03 PM 0
Share

so while messing around trying to get the class to remove the gameObject I used a destoryImmediate and lost some stuff permanently, so unfortunately I'll be starting from scratch with this one, clean slate and all that.

In regard to your solution surely the auto-complete would only function if the reference was working as the thing it was auto-completing was in another class? if there was no reference then how did it no what to talk to? Or can unity access other classes properties without a reference but they won't function unless you do reference?

Thanks for taking it slow and giving me the explanation I am sure I will be referring back to this as I rebuild this script!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by spooneystone · Nov 04, 2015 at 11:48 PM

Try the second part like this castInt castScript = castAccess.GetComponent();

Comment
Add comment · Show 5 · 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 spooneystone · Nov 04, 2015 at 11:51 PM 0
Share

Get component(); I mean

avatar image spooneystone · Nov 04, 2015 at 11:53 PM 0
Share

castInt castScript = castAccess.GetComponent();

avatar image spooneystone · Nov 04, 2015 at 11:57 PM 1
Share

castInt castScript = castAccess.GetComponent<castInt>();

avatar image spooneystone · Nov 04, 2015 at 11:57 PM 0
Share

Sorry about that.

avatar image Cripstat spooneystone · Nov 05, 2015 at 01:55 PM 0
Share

its no problem :) Sorry for taking a while to reply but i'm still getting "object reference not set to and instance of an object" error when I try and call the function, the code itself is working out but some reference is still failing.

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

34 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 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

Why is the this object referenced to itself? 1 Answer

Instatiated object not being referenced in Start function? 2 Answers

Getting Int value from script to other script 2 Answers

The referenced script on this Behaviour is missing - on any new script 0 Answers

Referencing instances of scripts based on the GameObject they are attached to. 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