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 Megomon · Jan 09, 2018 at 01:48 PM · getcomponentcomponentobjectsreferencing

Get Component isn't working too well

Hello again Unity Community;

I am creating my first game (not following any tutorials and etc.) and I seem to have difficulties with the command: GetComponent<>;

Down bellow is the section of the code where I worked on the command:

             if (i == 2) {
                 GetComponent<WPmsslCTRL> ().parabolCtrl();
                 i = 0;
             }
         }

Which should run the the function:

     public void parabolCtrl() 
     {
         for (x = -50 ; x <= 50; x++) 
         {
             waypointNum.Add (Instantiate (wayPoint, new Vector3 (x,- (0.09f)*(x*x)+10, 0), Quaternion.identity)); // just like y= ax^2 + bx + c
 
         } 
     }

Unfortunately, I keep getting this error:

NullReferenceException: Object reference not set to an instance of an object testSpawn.Update () (at Assets/Script/testSpawn.cs:58)

I made sure that the component is attached to a game object and both the component and the game object are set active. I am not clear on the issue with my getComponent.

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

1 Reply

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

Answer by sisse008 · Jan 09, 2018 at 01:54 PM

if the WPmsslCTRL component is attached to a different gameobject (different than the gameobject this script is attached to), than you need to reference that gameobject and only then reference the WPmsslCTRL component:

 public GameObject go;

  void YourFunction()
  {
         WPmsslCTRL _ctrl =  go.GetComponent<WPmsslCTRL> ();
         if(_ctrl != null)
          {
                 _ctrl.parabolCtrl();
          }
  }

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 Megomon · Jan 09, 2018 at 02:02 PM 0
Share

*Just realized

Thanks so much, I can't believe I spent an hour trying to resolve this issue, guess I need to work more in coding logic and Unity and get used to it

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

74 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

Related Questions

How do you use a Component slot? 1 Answer

Game Manager can't decide what the instance of the object is despite just making an instance of it 1 Answer

Toogle Parent to Camera Function - Google Cardboard 0 Answers

How to test what material is applied to an object. 0 Answers

How to correctly handle NullReferenceException when using GetComponent<>? 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