Search Unity

getComponent does not work?

Discussion in 'Scripting' started by Glemau, Oct 14, 2015.

  1. Glemau

    Glemau

    Joined:
    Jul 24, 2015
    Posts:
    37
    Hello everyone,

    Can anyone tell whats wrong with my Script?
    The Void Start does not Work.
    I was trying to enable/disable Script Components with this Script
    So i guess the Scriptable Object does not belong there, I was just trying out.
    For more Information Comment but ill answer tomorrow.

    ~Glemau

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Switch : MonoBehaviour {
    5.  
    6.     public GameObject gb;
    7.     public GameObject go;
    8.     public Transform trans;
    9.     private bool inCar = false;
    10.     public Transform door;
    11.     private ScriptableObject Control;
    12.     private ScriptableObject UserControl;
    13.     private ScriptableObject Audio;
    14.  
    15.     void Start () {//This part does not work!
    16.         Control = gb.GetComponent<Car Controller>(ScriptableObject);//Unexpected Symbol Controller
    17.         UserControl = gb.GetComponent<Car UserControl> (ScriptableObject);//Unexpected Symbol User
    18.         Audio = gb.GetComponent<Car Audio> (ScriptableObject);//Unexpected Symbol Object
    19.     }
    20.  
    21.     void Update () {
    22.  
    23.         if (Input.GetKeyDown (KeyCode.F) & inCar == false) {
    24.  
    25.             inCar = true;
    26.             go.SetActive(true);
    27.          
    28.  
    29.         }
    30.         else if (Input.GetKeyDown (KeyCode.F) & inCar == true) {
    31.          
    32.             inCar = false;
    33.             go.SetActive(false);
    34.             trans.transform.position = door.transform.position;
    35.  
    36.          
    37.         }
    38.  
    39.  
    40.     }
    41. }
    42.  
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    GetComponent works fine, as it always has. Your syntax, however, is wrong. You don't put anything in the parentheses; it's just GetComponent<Type>(). When using the generic version, there are no parameters.

    --Eric
     
  3. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    In addition, your class names wont work because you have spaces in them. "CarAudio" not "Car Audio". Syntax is important.

    Furthermore, scriptableObjects are not components, so you could not use GetComponent to find them. Scriptable objects exist because they work with serialization, so you should use serialization to allocate them.
     
  4. Glemau

    Glemau

    Joined:
    Jul 24, 2015
    Posts:
    37
    S

    So how could I disable those scripts? (Maybe an example I dont get what you said there.)
     
  5. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Sounds to me like you should start with some foundational skills and work your way up! Learn some C#, learn about how unity works, and these kinds of problems will be easy for you.

    Start here: https://unity3d.com/learn/tutorials/topics/scripting
     
  6. Glemau

    Glemau

    Joined:
    Jul 24, 2015
    Posts:
    37
    You're so helpful...
     
    henriquepro likes this.
  7. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667