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 /
avatar image
0
Question by Sniper1RDR · Jul 04, 2017 at 12:26 PM · scripting problem

Door script problem

When the door is not visible in the visibility zone, it is necessary to move the cursor away from the object and there is an opening

Script door

using UnityEngine; using System.Collections;

public class DoorScript : MonoBehaviour {

 //GENERAL SETTINGS
 [Header("General Settings")]
 [Tooltip("How close the player has to be in order to be able to open/close the door.")]
 public float Reach = 4.0F;
 [HideInInspector] public bool InReach;

 //UI SETTINGS
 [Header("UI Settings")]
 [Tooltip("The image or text that will be shown whenever the player is in reach of the door.")]
 public GameObject TextPrefab; //A text element to display when the player is in reach of the door.
 [HideInInspector] public GameObject TextPrefabInstance; //A copy of the text prefab to prevent data corruption.
 [HideInInspector] public bool TextActive;

 public AudioSource openDoor;
 public AudioSource closeDoor;
 public bool open;
 public bool close;
 public bool inTrigger;
 

  void Start ()
 {


 }
 void Update()
 {
     //Set origin of ray to 'center of screen' and direction of ray to 'cameraview'.
     Ray ray = Camera.main.ViewportPointToRay(new Vector3 (0.5F, 0.5F, 0F));

     RaycastHit hit; //Variable reading information about the collider hit.

     //Cast ray from center of the screen towards where the player is looking.
     if (Physics.Raycast(ray, out hit, Reach))
     {
         if (hit.collider.tag == "Door")
         {
             InReach = true;

             //Display the UI element when the player is in reach of the door.
             if(TextActive == false && TextPrefab != null)
             {
                 TextPrefabInstance = Instantiate(TextPrefab);
                 TextActive = true;
                 TextPrefabInstance.transform.SetParent(transform,true); //Make the player the parent object of the text element.
             }

             //Give the object that was hit the name 'Door'.
             GameObject Door = hit.transform.gameObject;

             //Get access to the 'Door' script attached to the object that was hit.
             Door dooropening = Door.GetComponent<Door>();

             {
                 //Open/close the door by running the 'Open' function found in the 'Door' script.
                 if (dooropening.Running == true) StartCoroutine (hit.collider.GetComponent<Door>().Open());
             }
         }
         else
         {
             InReach = false;

             //Destroy the UI element when Player is no longer in reach of the door.
             if(TextActive == true)
             {
                 DestroyImmediate(TextPrefabInstance);
                 TextActive = false;
             }
          }
     }

     else
     {
         InReach = false;

        //Destroy the UI element when Player is no longer in reach of the door.
         if(TextActive == true)
         {
              DestroyImmediate(TextPrefabInstance);
             TextActive = false;
         }
     }

     
     if (inTrigger)
     {
         if (close)
         {
 
             {
                 if (Input.GetKeyDown(KeyCode.E))
                 {
                     open = true;
                     close = false;
                     openDoor.Play();
                 }
             }
         }
         else
         if (open)
         {
             if (Input.GetKeyDown(KeyCode.E))
             {
                 close = true;
                 open = false;
                 closeDoor.Play();
             }
         }
     }
 
     if (open)
     {
         transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0.0f, -90.0f, 0.0f), Time.deltaTime * 200);
       
     }
    
    if (close)
     {
         transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0.0f, 0.0f, 0.0f), Time.deltaTime * 200);
       
     }  
 }

}

Help me decide

Error here (NullReferenceException: Object reference not set to an instance of an object) {

                 if (dooropening.Running == true) StartCoroutine (hit.collider.GetComponent<Door>().Open());
             }
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

Answer by merkaba48 · Jul 04, 2017 at 07:12 PM

I don't understand your problem as you've described it, but fix that nullref exception first in case that's the cause of your issue.

As you've already confirmed there is a hit, for some reason it's probably not finding the Door component. Is the Door script actually on the gameobject with the collider, or is it a child, or parent? If a parent, then use hit.collider.transform.parent.GetComponent<Door>().Open(), or if it's a child, use hit.collider.GetComponentInChildren<Door>().Open().

Alternatively, it's possible you accidentally have a collider in the scene with the 'Door' tag but no Door component.

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

103 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

Related Questions

How to make script affect only one this game object 2 Answers

error Int to string?Assets/Scripts/MyConnect.cs(61,60): error CS0029: Cannot implicitly convert type `int' to `string' 1 Answer

How can i limit object dragging area in my inventory? 0 Answers

Door open script opens all the doors in the scene 3 Answers

Make an object a certain distance from me. 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