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
  4. Dismiss Notice

change variable of other script

Discussion in 'Scripting' started by ben7bf, Jan 22, 2021.

  1. ben7bf

    ben7bf

    Joined:
    Jan 22, 2021
    Posts:
    18
    Hello

    The player(Player) should change the speed of the camera(CameraController).
    But when I want to get the variable from the player with GetComponent, it doesn't work.
    Can someone help me please?

    Player:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [RequireComponent(typeof(AudioSource))]
    4.  
    5. public class Player : MonoBehaviour
    6. {
    7.     public int speedx = 4;
    8.     public int speedy = 3;
    9.     public GameObject OnDeathText;
    10.     public GameObject Camera;
    11.  
    12.     AudioSource LevelAudio;
    13.  
    14.     void Start()
    15.     {
    16.         OnDeathText.SetActive(false);
    17.         LevelAudio = GetComponent<AudioSource>();
    18.         LevelAudio.Play(0);
    19.     }
    20.     void Update()
    21.     {
    22.         if (Input.GetKey(KeyCode.Return))
    23.         {            
    24.             Vector3 new_position = new Vector3(transform.position.x + (speedx * Time.deltaTime), transform.position.y + (speedy * Time.deltaTime), 0);
    25.             transform.position = new_position;
    26.         }
    27.  
    28.         else
    29.         {
    30.             Vector3 new_position = new Vector3(transform.position.x + (speedx * Time.deltaTime), transform.position.y - (speedy * Time.deltaTime), 0);
    31.             transform.position = new_position;
    32.         }
    33.        
    34.     }
    35.  
    36.     void OnTriggerEnter2D(Collider2D collision)
    37.     {
    38.         if (collision.CompareTag("Level Touchable"))
    39.         {
    40.             Vector3 new_position = new Vector3(Camera.transform.position.x, Camera.transform.position.y, -1);
    41.             OnDeathText.transform.position = new_position;
    42.             OnDeathText.SetActive(true);
    43.             Camera.GetComponent<CameraController>.speed = 0;
    44.         }
    45.     }
    46. }
    CameraController:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CameraController : MonoBehaviour
    4. {
    5.     public int speed = 4;
    6.     private void Start()
    7.     {
    8.         Application.targetFrameRate = 60;
    9.     }
    10.  
    11.     void Update()
    12.     {
    13.         Vector3 new_position = new Vector3(transform.position.x + (speed * Time.deltaTime), transform.position.y, -10);
    14.         transform.position = new_position;
    15.     }
    16. }
     
  2. KyleTheCoder420

    KyleTheCoder420

    Joined:
    May 30, 2020
    Posts:
    3
    Try renaming your Camera game object to "camera", there already is a UnityEngine class called Camera to interface with cameras.
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. [RequireComponent(typeof(AudioSource))]
    4. public class Player : MonoBehaviour
    5. {
    6.     public int speedx = 4;
    7.     public int speedy = 3;
    8.     public GameObject OnDeathText;
    9.     public GameObject camera;
    10.     AudioSource LevelAudio;
    11.     void Start()
    12.     {
    13.         OnDeathText.SetActive(false);
    14.         LevelAudio = GetComponent<AudioSource>();
    15.         LevelAudio.Play(0);
    16.     }
    17.     void Update()
    18.     {
    19.         if (Input.GetKey(KeyCode.Return))
    20.         {          
    21.             Vector3 new_position = new Vector3(transform.position.x + (speedx * Time.deltaTime), transform.position.y + (speedy * Time.deltaTime), 0);
    22.             transform.position = new_position;
    23.         }
    24.         else
    25.         {
    26.             Vector3 new_position = new Vector3(transform.position.x + (speedx * Time.deltaTime), transform.position.y - (speedy * Time.deltaTime), 0);
    27.             transform.position = new_position;
    28.         }
    29.      
    30.     }
    31.     void OnTriggerEnter2D(Collider2D collision)
    32.     {
    33.         if (collision.CompareTag("Level Touchable"))
    34.         {
    35.             Vector3 new_position = new Vector3(camera.transform.position.x, camera.transform.position.y, -1);
    36.             OnDeathText.transform.position = new_position;
    37.             OnDeathText.SetActive(true);
    38.             camera.GetComponent<CameraController>.speed = 0;
    39.         }
    40.     }
    41. }
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
  4. ben7bf

    ben7bf

    Joined:
    Jan 22, 2021
    Posts:
    18
    @KyleTheCoder420
    Thanks for the help. It works.
    Code (CSharp):
    1. cameracontroller.GetComponent<CameraController>().speed = 0;
    The brackets were missing. :oops:
     
  5. KyleTheCoder420

    KyleTheCoder420

    Joined:
    May 30, 2020
    Posts:
    3
    No problem! :D
     
  6. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    Yep, GetComponent is a method, hence the ().