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

Disabling a script using another script (Different game objects)

Discussion in 'Scripting' started by Deleted User, Aug 23, 2015.

  1. Deleted User

    Deleted User

    Guest

    I want to disable a script attached to a camera using a script attached to the player
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MainRestart : MonoBehaviour {
    5.  
    6.     private BlockSizeAndPosition DisableSpawningBlocks;
    7.     private PlayerMovement DisablePlayerMovement;
    8.     private MainRestart ThisScript;
    9.  
    10.     public GameObject DisableCameraFollow;
    11.  
    12.     void Awake() {
    13.         DisableSpawningBlocks = GetComponent<BlockSizeAndPosition>();
    14.         DisablePlayerMovement = GetComponent<PlayerMovement>();
    15.         DisableCameraFollow = GetComponent<CameraFollow>();
    16.         ThisScript = GetComponent<MainRestart>();
    17.         DisableCameraFollow = GameObject.FindWithTag ("MainCamera").GetComponent<CameraFollow>(); //I'm trying to get the CameraFollow script using this line
    18.     }
    19.  
    20.     void OnTriggerEnter2D(Collider2D OtherObject) {
    21.         if(OtherObject.gameObject.CompareTag("Enemy")) {
    22.             DisableSpawningBlocks.enabled = false;
    23.             DisablePlayerMovement.enabled = false;
    24.             DisableCameraFollow.enabled = false; //It won't let me use the "enabled"
    25.             Destroy (gameObject);
    26.             ThisScript.enabled = false;
    27.         }
    28.     }
    29.  
    30.     void Update() {
    31.         if (transform.position.x > 2.62f || transform.position.x < -2.62f) {
    32.             DisableSpawningBlocks.enabled = false;
    33.             DisablePlayerMovement.enabled = false;
    34.             DisableCameraFollow.enabled = false; //Same problem with "enabled" here
    35.             Destroy (gameObject);
    36.             ThisScript.enabled = false;
    37.         }
    38.     }
    39.  
    40. }
     
  2. lineupthesky

    lineupthesky

    Joined:
    Jan 31, 2015
    Posts:
    92
    I'm not sure but it might be because you try to get the object on Awake, maybe the object is inactive in the beginning ? Just to test it, put your line where you get the CameraFollow component, on Start. And then :

    Code (CSharp):
    1. Debug.Log(DisableCameraFollow);
    Do this, just so that we can see if the component is null or not.
     
  3. Deleted User

    Deleted User

    Guest

    I tried it but I'm getting these errors (got it also before trying what you said just forgot to mention):
     
  4. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,905
    Couldn't you do
    Code (CSharp):
    1. WantedObj.GetComponent<WantedScript>().enabled=false;
    ?
     
  5. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,905
    What do you want to do as opposed to that? All you asked was for how to disable a script with another one