Search Unity

Accessing a class from another script (again)

Discussion in 'Scripting' started by jeannel, Jun 12, 2018.

  1. jeannel

    jeannel

    Joined:
    Jun 26, 2017
    Posts:
    17
    Hi all, I red a few thread but I don't get why it's not working here :

    I'm setting some rotation (quaternion) in a shipControls class
    and want to get these quaternions in a shipMotors class.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class shipControls : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     private float speed = 10 ;
    9.     [SerializeField]
    10.     private float angularSpeed = 5.0f;
    11.     private float horizontalAccumulate=0.0f;
    12.     private Rigidbody rb;
    13.     private Quaternion target;
    14.     public Quaternion Xport;
    15.    
    16.  
    17.  
    18.     // Use this for initialization
    19.     void Awake()
    20.     {
    21.         rb=GetComponent<Rigidbody>();
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         float horizontal = Input.GetAxis("Horizontal") ;
    28.         horizontalAccumulate += horizontal;
    29.         float vertical = Input.GetAxis("Vertical");
    30.        
    31.      
    32.         target = Quaternion.Euler(0, horizontalAccumulate*angularSpeed, -horizontal*angularSpeed ) ;
    33.         transform.rotation = target;
    34.         Xport = target;
    35.      
    36.  
    37.         rb.velocity = Vector3.Lerp( transform.forward *vertical* Time.deltaTime * speed, Vector3.zero, Time.deltaTime);
    38.  
    39.  
    40.        
    41.     }
    42.  
    43. }
    44.  
    and for the shipMotors

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class shipMotors : MonoBehaviour
    6. {
    7.     public Quaternion toto;
    8.     shipControls shipControls;
    9.    
    10.     // Use this for initialization
    11.     void Start()
    12.     {
    13.        
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void LateUpdate()
    18.     {
    19.        
    20.         toto=shipControls.Xport;
    21.         //transform.rotation = toto;
    22.  
    23.     }
    24. }
    25.  
    it gives a {System.NullReferenceException} error, which I don't understand since when I type, the auto completition works...

    I'm really beginner and having classes that "talks" to each over is a new thing for me.

    Thank you for your enlightment :)
     
  2. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    You aren't assigning anything to shipControls. You probably add a shipControls = GetComponent<shipControls>();

    I'm surprised it even compiles, you shouldn't have variable names the same as your class names.
     
  3. jeannel

    jeannel

    Joined:
    Jun 26, 2017
    Posts:
    17
    Hey there and thank you !
    I'm not getting what you mean.
    Am-I wrong in the shipMotors class ? Can you detail a little bit more ?
     
  4. jeannel

    jeannel

    Joined:
    Jun 26, 2017
    Posts:
    17
    So I found a way, referencing the GameObject, getting (GetComponent) the script and finally accessing the variable
    Code (CSharp):
    1. ublic class shipMotors : MonoBehaviour
    2. {
    3.     private float y;
    4.    
    5.    
    6.     // Use this for initialization
    7.     void Start()
    8.     {
    9.         y=GameObject.Find("PlayerShip").GetComponent<shipControls>().shipY;
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void LateUpdate()
    14.     {
    15.  
    16.         y = GameObject.Find("PlayerShip").GetComponent<shipControls>().shipY;
    17.        
    18.         transform.rotation = Quaternion.Euler(y*20, 0, 0);
    19.  
    20.     }
    21. }
     
  5. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    289
    What he meant is that the field shipControls has the same name as the class shipControls.

    Your classes should always start with caps

    Code (CSharp):
    1. ShipControls shipControls;
    and on the other hand you can just asign on the editor wich ShipControl you want to use on your ShipMotor.
     
  6. jeannel

    jeannel

    Joined:
    Jun 26, 2017
    Posts:
    17
    Ah thank's for the cap-class rule info :)
    How would you "asign on the editor wich ShipControl you want to use on your ShipMotor" ?
     
  7. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    289
    If you make a variable public, or use the [SerializeField] attribute on it, the field will show on the component's inspector. You then drag and drop the game object containing the type of component that field needs.