Search Unity

C# Scripts not working

Discussion in 'Scripting' started by Pranav_Redstoneinvente, Nov 12, 2019.

  1. Pranav_Redstoneinvente

    Pranav_Redstoneinvente

    Joined:
    Apr 13, 2018
    Posts:
    121
    Hi, I have created a C# script for my game in unity 2019.2.0f1 and when it has finished compiling, i. Tried to drop a GameObject to it to assign it as a GameObject variable, but it doesn't let me do that, I cannot assign any GameObject to it.... Also when I tried : GameObject.setActive(true); it doesn't let me , I don't have the option to do it....
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Show us your code (please use code tags). then perhaps describe a Little bit more how you attached the script to a game object (not the other way round). Maybe describe also what you expect to see, what you see instead, and why you think that is not what you want. I'm sure we can sort this one out in no time at all :)
     
  3. paragyogi

    paragyogi

    Joined:
    Oct 4, 2018
    Posts:
    19
    We drop script to game object in inspector window not other way round
     
  4. Pranav_Redstoneinvente

    Pranav_Redstoneinvente

    Joined:
    Apr 13, 2018
    Posts:
    121
    Here is the code This is not the only script im having the problem:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DoorManager : MonoBehaviour
    6. {
    7.     public Key Requiredkey;
    8.     public Key TakenKey;
    9.     string KeyName;
    10.     public bool IsLocked;
    11.     public GameObject TDoor;
    12.     bool IsOpen;
    13.  
    14.     public GameObject OpenDoorDisp;
    15.  
    16.  
    17.     private void Start()
    18.     {
    19.         if (IsLocked == true)
    20.         {
    21.             KeyName = Requiredkey.KeyName;
    22.         }
    23.     }
    24.  
    25.     public void Open()
    26.     {
    27.         if (IsLocked == true)
    28.         {
    29.             if (TakenKey.KeyName == KeyName)
    30.             {
    31.                 if (IsOpen == true)
    32.                 {
    33.                     this.gameObject.GetComponent<Animation>().Play("DoorClose");
    34.                 }
    35.                 else
    36.                 {
    37.                     this.gameObject.GetComponent<Animation>().Play("DoorOpen");
    38.                 }
    39.                 IsOpen = !IsOpen;
    40.             }
    41.             else
    42.             {
    43.                 //Display Error
    44.             }
    45.         }
    46.         else
    47.         {
    48.             if (IsOpen == true)
    49.             {
    50.                 this.gameObject.GetComponent<Animation>().Play("DoorClose");
    51.             }
    52.             else
    53.             {
    54.                 this.gameObject.GetComponent<Animation>().Play("DoorOpen");
    55.             }
    56.             IsOpen = !IsOpen;
    57.         }
    58.     }
    59.  
    60.  
    61.     public bool PlayerNear = false;  //Triggers when player is inside given range
    62.     public void OnTriggerEnter(Collider other)
    63.     {
    64.         if (other.gameObject.tag == "Player")
    65.         {
    66.             PlayerNear = true;
    67.         }
    68.     }
    69.  
    70.     public void OnTriggerExit(Collider other)
    71.     {
    72.         if (other.gameObject.tag == "Player")
    73.         {
    74.             PlayerNear = false;
    75.             //Display is hidden
    76.         }
    77.     }
    78.  
    79.     private void Update()
    80.     {
    81.         if (PlayerNear == true)
    82.         {
    83.             if (Input.GetKeyDown(KeyCode.E))
    84.             {
    85.                 Open();
    86.             }
    87.         }
    88.     }
    89. }
    90.  
    prob.PNG

     
  5. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    So what is the problem? Your imagery seems to indicate some issues with autocompletion, hardly a Unity issue.
     
  6. Pranav_Redstoneinvente

    Pranav_Redstoneinvente

    Joined:
    Apr 13, 2018
    Posts:
    121
    Found the problem, actually i had a script named GameManager and for whatever reason, its namespace was GameObject