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

How to enable a variable from one script to another?

Discussion in 'Scripting' started by Deleted User, Nov 18, 2015.

  1. Deleted User

    Deleted User

    Guest

    Hey guys so basically I got 2 scripts. My first person controller is holding a piece of paper, but I disable it at the start since Unity can't find gameObjects that are disabled from the start. I got everything working except I can't enable it, this is my code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class CollisionWithPlane : MonoBehaviour
    4. {
    5.    
    6.  
    7.     void OnTriggerExit(Collider coll)
    8.    {
    9.        if (coll.GetComponent<PickUpItem>().isKeyPickedUp)
    10.        {
    11.            Debug.Log("true");
    12.  
    13.            transform.position = new Vector3(56f, 0.064f, 94f);
    14.        }
    15.    }
    16.     void OnTriggerEnter(Collider col)
    17.     {
    18.         if (col.GetComponent<PickUpItem>() != null && col.GetComponent<PickUpItem>().isKeyPickedUp)
    19.         {
    20.             Debug.Log("Pause");
    21.             //This will disable the MouseLook script
    22.             MouseLook.instance.enabled = false;
    23.             //This will disable the CharacterMotor script
    24.             CharacterMotor.instance2.enabled = false;
    25.             //This will disable the FPSInputController script
    26.             FPSInputController.instance1.enabled = false;
    27.             //here is where I try to enable the script. I make a instance of the other class, and the variable is called "sign"
    28.             PickUpItem inst = new PickUpItem();
    29.             inst.GetComponent<MeshRenderer>().enabled = true;
    30.         }      
    31.     }
    32. }
    33.  
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you can't get the component of a component... that makes no sense.

    Is PickUpItem a gameobject or a component? for the majority of that script you're dealing with it as a component, then you try to create a new object of that class and then get a component on that object (that isn't the way you create components, and again, components don't have components you access in that manner).

    Perhaps you could explain the hierarchy you have... ?
     
    Kiwasi likes this.
  3. Deleted User

    Deleted User

    Guest

    PickUpItem is a script. Here it is

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class PickUpItem : MonoBehaviour
    4. {
    5.     public bool isKeyPickedUp = false;
    6.  
    7.     public static CharacterController cc;
    8.     // Update is called once per frame
    9.  
    10.     public static GameObject sign;
    11.  
    12.     void Awake()
    13.     {
    14.         sign = GameObject.Find("QuickPause");
    15.         sign.GetComponent<MeshRenderer>().enabled = false;
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.         //this is the raycast variable for detecting the key
    21.         RaycastHit hitBox;
    22.         //if the phyiscs raycast its position looking forwardm hitbox to store the data and a length of 2
    23.         if (Physics.Raycast(transform.position, transform.forward, out hitBox, 2))
    24.         {
    25.             //if the raycast is hitting the tag key and pressing r and iskeypickedup is false then
    26.             if (hitBox.collider.gameObject.tag == "Key" && Input.GetKeyDown(KeyCode.R) && isKeyPickedUp == false)
    27.             {
    28.                 print("HELLO");
    29.                 //find the key destroy it
    30.                 GameObject keyObject = GameObject.Find("Key");
    31.                 GameObject.Destroy(keyObject);
    32.  
    33.                 //this is true now since the key is picked up
    34.                 isKeyPickedUp = true;
    35.  
    36.                 print("door destroyed!");
    37.                 //find bedroom door destroy it
    38.                 GameObject BedroomDoor = GameObject.Find("OBedroomDoor");
    39.                 GameObject.Destroy(BedroomDoor);            
    40.             }
    41.         }            
    42.     }    
    43. }
     
  4. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    if the MeshRenderer is on the same object like your PickUpItem script, than you can do

    Code (CSharp):
    1. void OnTriggerEnter(Collider col)
    2.     {
    3.         if (col.GetComponent<PickUpItem>() != null && col.GetComponent<PickUpItem>().isKeyPickedUp)
    4.         {
    5.             Debug.Log("Pause");
    6.  
    7.             MouseLook.instance.enabled = false;
    8.  
    9.             CharacterMotor.instance2.enabled = false;
    10.  
    11.             FPSInputController.instance1.enabled = false;
    12.  
    13.             col.GetComponent<MeshRenderer>().enabled = true;
    14.         }    
    15.     }
    why you have instance1 , instance2 ? strange :D
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. col.GetComponent<PickUpItem>().sign.GetComponent<MeshRenderer>().enabled = true;
    3.  
    really should just make a function on the pickupitem to handle it, huge opening for a null ref exception here :)
     
    Kiwasi likes this.
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You actually can. Why is a little more esoteric. But you can call GetComponent on any Component or GameObject.
     
  7. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Well, when you call GetComponent on a component, it's actually being called on that components gameObjects. So think of it as 'GetSiblingComponent'
     
    Kiwasi likes this.
  8. Deleted User

    Deleted User

    Guest

    There is a plane attatched to my first person controller, which has the meshrender on it. Which 'm trying to enable back.
     
  9. aLovedHater

    aLovedHater

    Joined:
    Oct 12, 2014
    Posts:
    16
    if you're disabling a mesh renderer or a component on your object use

    Code (CSharp):
    1. gameObject.GetCompnent<MeshRenderer>().enabled = true; // or false
    if the script is not attached to the object you have to make sure to make a GameObject variable and set it to the object with the mesh renederer. Or if it's on a parent or child object you have to use getComponentInParent or GetComponentInChildren

    also if that plane has a collider you might want to disable that to. (do the same thing but instead of mesh renderer use your collider component)
     
  10. Deleted User

    Deleted User

    Guest

    Ok thanks