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 Newbyscript · Nov 09, 2013 at 05:46 PM · get component

null reference exception on get component

The code is giving me a null reference error and i dont understand why? The error is apparently on the 2nd line of ProjectileEquip function. been trying to figure it out for far to long.

 using UnityEngine;
 using System.Collections;
 
 public class SubMenuGUI : MonoBehaviour {
     
     public GUIStyle myGUI;
     public int selGridInt = 0;
     public string[] weapons = new string[] {"Plasma", "RailGun"};
     
     private scriptPlayer scriptplayer;
     
     void Awake(){
         scriptplayer = GetComponent<scriptPlayer>();
         
         
     }
     
     void OnGUI(){
         
         GUI.Box(new Rect(0,0,Screen.width,Screen.height),"Customize Your Ship", myGUI);
         selGridInt = GUI.SelectionGrid(new Rect(25, 25, 150, 30), selGridInt, weapons, 2);
         
         
         ProjectileEquip();
         
         
         if(GUI.Button(new Rect(Screen.width / 2 - 50, Screen.width / 2 + 150,100, 50), "Enter World"))
             Destroy(this);
     }
     
     void ProjectileEquip(){
         if(selGridInt == 0){
             scriptplayer.plasma = true;
         }
         if(selGridInt == 1)
             print("RailGun");
     }
     
 }
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

Answer by DeepakSingh491 · Nov 09, 2013 at 05:54 PM

Hey! its not getting the right script check that you have script name right..:)

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
0

Answer by DeepakSingh491 · Nov 09, 2013 at 05:54 PM

int the script getcomponent you should have written getcomponent(script)

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
0

Answer by Newbyscript · Nov 09, 2013 at 06:25 PM

the name of the script is scriptPlayer

(its not letting me add comment so i had to say in answers)

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
0

Answer by vikitosss · Oct 11, 2020 at 05:14 PM

How to Get 2 different Components to one variable ? srcFirst.cs / srcSecond.cs


public ????? src;

void start(){

  src = gameObject.GetComponent<srcFirst>();
  if(!src) src = gameObject.GetComponent<srcSecond>();

}

I get "Embedded statement cannot be a declaration or labeled statement" But I need only one variable..

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

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

18 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

Related Questions

Script to recive varible from toher script and do something with it 1 Answer

How to Link Scripts Correctly? 1 Answer

Calling Functions Within A Script? 1 Answer

Proper syntax for GetComponent 3 Answers

How to access a variable from another class (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