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 $$anonymous$$ · Jul 20, 2016 at 02:05 PM · c#classinheritanceclassesclass object

Custom class, Null Reference Exception

I've created a custom class but for some reason it throws a null exception when I try to run the code that was otherwise fine. The problem is on the line with _renderer[clone] statement.

 private Machine[] _machineClone;

 void Start()
     {
         // Set machines and properties array
         _machineClone = new Machine[machineCount];
         ...
         // Spawn machine
         _machineClone[count] = Instantiate(machine, pos, Quaternion.identity) as Machine;
         // Get machine renderer to use later
         _renderer[count] = _machineClone[count].gameObject.GetComponent<Renderer>() as Renderer;
         ...
 public class Machine : MonoBehaviour
 {
     public GameObject gameObject;
 }
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

4 Replies

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

Answer by $$anonymous$$ · Jul 21, 2016 at 01:56 PM

So what cured this is I had to change my clone assignment line to

 _machineClone[count] = ((GameObject)Instantiate(machine, pos, Quaternion.identity)).GetComponent<Machine>();

as pointed out by @M-Hanssen and then move my Machine class to a separate script and attach it to the prefab and set the GameObject.

Comment
Add comment · 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
2

Answer by M-Hanssen · Jul 21, 2016 at 08:07 AM

Change your line to this:

 _machineClone[count] = ((GameObject)Instantiate(machine, pos, Quaternion.identity)).GetComponent<Machine>();
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 $$anonymous$$ · Jul 21, 2016 at 08:20 AM 0
Share

This line still throws a null exception

 _renderer[count] = _machineClone[count].gameObject.GetComponent<Renderer>() as Renderer;

avatar image CreativeStormEntertainment · May 24, 2019 at 06:35 PM 0
Share

This one worked for me, thanks!

avatar image
1

Answer by rmassanet · Jul 20, 2016 at 02:36 PM

Instantiatereturns a reference to the GameObject just instantiated, but you are casting it to your Machine script, which is not a GameObject and probably causes the null value you are observing.

Comment
Add comment · Show 4 · 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 JamesLawrenceMyQVO · Jul 20, 2016 at 02:39 PM 0
Share

That's seems likely. Ins$$anonymous$$d of "as $$anonymous$$achine;" do ".GetComponent();"

avatar image $$anonymous$$ JamesLawrenceMyQVO · Jul 21, 2016 at 08:01 AM 0
Share

That doesn't compile. 'Object does not contain a definition for 'GetComponent'.'

avatar image $$anonymous$$ · Jul 21, 2016 at 08:02 AM 0
Share

How do I go around this problem?

avatar image rmassanet $$anonymous$$ · Jul 21, 2016 at 01:22 PM 0
Share

Cast it to GameObject and then call GetComponent.

avatar image
0

Answer by JamesLawrenceMyQVO · Jul 20, 2016 at 02:18 PM

I'm guessing it is throwing the exception on the line with the GetComponent()? It is hard to tell what might be causing it with the limited look at the code, since I'm not sure how things are instantiated, but could it be an off by one error with count vs machineCount? _machineClone would be indexed from 0 up to machineCount - 1, but maybe your count is going all the way up to machineCount?

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 $$anonymous$$ · Jul 21, 2016 at 08:03 AM 0
Share

Yeah, that's the line. The count is fine, it worked before I introduced the custom $$anonymous$$achine class.

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

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

Can't instantiate new class C# 0 Answers

Disable a group of scripts based on base class. 0 Answers

Randomizing and using Bool values 0 Answers

C# Find specific object by getting one of its variables 0 Answers

Accessing Variables inside classes inside other classes? 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