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 SHOOTERNsK · Mar 25, 2014 at 07:57 PM · getcomponentdoorkeyopen

Scripting issue with opening a door with a key.

So I am having an issue with my scripts that allow the player to pick up a key and open a door. The issue that I am having is coming from my Med Bay Door Manager script. The Med Bay Door Manager script is suppose to check the PickupItemScript on the FPS Controller to see whether the HasKey boolean variable is true or not when the player enters the trigger, but it's not working due to a NullReferenceException on line 16 of the Med Bay Door Manager script that's telling me that hasTheKey = itemPickup.HasKey object refernce is not set to an instance of an object. I am not sure how to fix this issue and it's taken me a few days just to get the game to play again trying to figure out how to access another script. Help would be greatly appreciated.

This is my PickupItemScript:

 using UnityEngine;
 using System.Collections;
 
 public class PickupItemScript : MonoBehaviour 
 {
     public bool HasKey = false;
 
     //player will get the key when the trigger is activated.
     void OnTriggerEnter(Collider other)
     {
         //when the player collides with the pickup(square/the key)
         //hasKey becomes true and the square is destroyed.
         if (other.gameObject.tag == "Pickup") 
         {
             HasKey = true;
             Destroy (other.gameObject);
         }        
     }
 }

This is my Med Bay Door Manager Script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class MedBayDoorManager : MonoBehaviour 
 {
     public Door door1;
     public Door door2;
     public PickupItemScript itemPickup;
     public bool hasTheKey;
 
     //initialization
     void Start () 
     {
         bool hasTheKey;
         itemPickup = GetComponent<PickupItemScript>();
         hasTheKey = itemPickup.HasKey;
     }
     
     // Doors open when trigger is activated.
     void onTriggerEnter () 
     {
         // If player has the key door1 will open.
         if (hasTheKey == true) 
         {
             door1.OpenDoor();
         }
 
         // Door2 will open when trigger is activated for now.
         // if statement will change when first if statement works.
         if (door2 != null) 
         {
             door2.OpenDoor ();
         }
     }
 }
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 SHOOTERNsK · Mar 25, 2014 at 11:31 PM 0
Share

Update: these two images show how I have the components and scripts set up. The image with the $$anonymous$$ed Bay Door $$anonymous$$anager Script is attached to the $$anonymous$$ed Bay Door object and the image with the FPS Controller Script is attached to the FPS Controller object.

bandicam 2014-03-25 19-00-54-766.jpg (44.8 kB)
bandicam 2014-03-25 19-01-50-672.jpg (39.4 kB)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by GMarco · Mar 25, 2014 at 08:49 PM

this may sound obvious, but i have to ask it. Where are those scripts attached? for what its wrote both are attached to the same object, and for the exception you are getting, this given object doesn't have an instance of the PickupItemScript.

You should make a reference to the parent of this script first, and then retrieve it (this of course assuming the visibility of the methods is public, elsewhere you should start using setters or messengers). Hope it helps

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 SHOOTERNsK · Mar 25, 2014 at 11:33 PM 0
Share

The $$anonymous$$ed Bay Door $$anonymous$$anager Script is attached to the $$anonymous$$ed Bay door object and the PickupItemScript is attached to the FPS Controller object.

avatar image
0

Answer by wraxul · Mar 25, 2014 at 08:57 PM

I'm not sure which scripts are attached to which objects, but if the game object with the MedBayDoorManager script attached does not also have a PickupItemScript attached, the GetComponent call is going to return null.

I assume both scripts are attached to the player? If that's the case, you'll most likely want to use GetComponentInChildren instead of GetComponent, provided the object with PickupItemScript is a child of the player. You'll also want to check the itemPickup.HasKey during the OnTriggerEnter rather than on Start provided that value can change during runtime.

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

22 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

Related Questions

Door opening with a key, my scripts and problem 1 Answer

Make doors open with different keys? 1 Answer

How to make 2 doors with 2 diffrent keys 2 Answers

Opening a door with a key 0 Answers

Using If functions to open door with a key. 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