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 /
This question was closed Dec 05, 2011 at 12:53 AM by aldonaletto for the following reason:

Answer found by the OP

avatar image
0
Question by TKEmil · Dec 05, 2011 at 12:50 AM · getcomponentnullreference

GetComponent returns null reference

Hello, I am trying to fill a wineglass. In order to fill it it must be empty, but Unity keeps throwing an exception saying null reference. I know what null reference is, but I have no idea why it is doing it here.

If I put in:

print(target)

It correctly returns the name of the object that the raycast hits.

This is the line that is throwing an error:

 if(target.GetComponent<WineGlass>().empty == true)


And this is the entire code:

 RaycastHit hit;
             
     if (Physics.Raycast(cameraTransform.position, cameraTransform.TransformDirection(Vector3.forward), out hit, 2.0f)) {
         
         GameObject target;
         
         target = hit.collider.gameObject;
                     
         switch (hit.collider.tag) {
             
         case "WineGlass":
             
             print("WAT");                
             if(GameManager.CurrentItem == GameManager.item.WineBottle){
                 if(target.GetComponent<WineGlass>().empty == true){ 
                     interfaceManager.WriteMessage("You fill the glass");
                     GameManager.FilledWineglassCount += 1;
                     target.SendMessage("FillGlass");
                 
                     if (GameManager.FilledWineglassCount == 10){
                         CompletePuzzle();
                         GameManager.PuzzleWineComplete = true;
                     }
                 }
             }
                             
             break;



And the following is the script attached to the object:

 public class WineGlass : MonoBehaviour {
     
     public bool empty;
     
     // Use this for initialization
     void Start () {
         
         empty = true;
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     
     public void FillGlass () {
         
         empty = false;
         print("Filling glass...");
         
     }
     
     public void EmptyGlass () {
         empty = true;
         print("Emptying glass...");    
     }
 }



I am sure it is just something that I don't understand, please help

Best Regards EKN

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 TKEmil · Dec 04, 2011 at 10:10 PM 0
Share

Sorry, rookie mistake, forgot to add the WineGlass script to the wineglass objects!

Leaving the question here because it may be usefull for others

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

GetComponent doesn't work anymore 2 Answers

NullReference when calling a function to another script 2 Answers

access enum values 0 Answers

GetComponent not working C# 1 Answer

A simple CS0309 problem... 2 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