Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

I have a problem with my script

Discussion in 'Scripting' started by Else1996, Jul 12, 2015.

  1. Else1996

    Else1996

    Joined:
    May 31, 2015
    Posts:
    6
    I have a problem with my script, the problem is it makes me repeatedly Probelm to accessing a function:

    I have a Key and a Door Object.
    When you take the key of the value to be implemented in the Door.cs.

    KeyObject.cs
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class KeyObject : MonoBehaviour {
    5.  
    6.     Door door;
    7.  
    8.     void OnCollisionEnter2D(Collision2D other)
    9.     {
    10.         if (other.gameObject.CompareTag("Player"))
    11.         {
    12.             door.GiveKey();
    13.             Destroy(gameObject);
    14.         }
    15.     }
    16. }
    17.  
    Door.cs
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Door : MonoBehaviour
    5. {
    6.     private bool Keys = false;
    7.  
    8.     void OnCollisionEnter2D(Collision2D other)
    9.     {
    10.         if (other.gameObject.CompareTag("Player"))
    11.         {
    12.             if (Keys == false)
    13.             {
    14.                 Debug.Log("Access DENIED");
    15.             }
    16.             else
    17.             {
    18.                 Destroy(gameObject);
    19.             }
    20.         }
    21.     }
    22.  
    23.     public void GiveKey()
    24.     {
    25.         Keys = true;
    26.     }
    27. }
    28.  
    greeting
    D. Elskamp
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    OK, so your KeyObject contains a Door, but you don't assign it a value. You need to couple the KeyObject to a Door object somehow. Either make the Door object public and assign a GameObject with a Door script through the Inspector or find it somehow for example with GameObject.Find(). Once you have a GameObject you can access the Door script like this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class KeyObject : MonoBehaviour {
    4.     public GameObject doorToOpen;
    5.     Door door;
    6.  
    7.    void Start() {
    8.       door = doorToOpen.GetComponent<Door>();
    9.    }
    10.  
    11.     void OnCollisionEnter2D(Collision2D other)
    12.     {
    13.         if (other.gameObject.CompareTag("Player"))
    14.         {
    15.             door.GiveKey();
    16.             Destroy(gameObject);
    17.         }
    18.     }
    19. }
     
  3. Else1996

    Else1996

    Joined:
    May 31, 2015
    Posts:
    6
    Thanks very much. It works perfectly.