Search Unity

Super beginner: how do I have 1 script react to data from another?

Discussion in 'Community Learning & Teaching' started by Toonami332, May 8, 2019.

  1. Toonami332

    Toonami332

    Joined:
    Oct 7, 2018
    Posts:
    1
    I'm brand new to Unity and have run across a problem where I need 1 script in the game to react to a variable taken from another. For the specifics, I'm making a 2D game that includes a point where the player falls down a tower and you steer him on the way down. The problem is that his speed is partially determined by his angle (he has a parachute and is able to angle up to slow down a bit). Instead of having him move I have a spawner below him sending obstacles for him to dodge. This means I need the spawned prefabs to know the player's current angle so I can use it to determine their speed. I already save the player's angle as a public variable but I don't know how to call on it from the other script. I thought this would be an easy thing to find an answer to but I've watched several tutorials and haven't found it.


    Any help would be appreciated. Thank you!
     
  2. Siegewolf

    Siegewolf

    Joined:
    Jun 15, 2018
    Posts:
    56
    If I understand what you are trying to make, there are number of ways to do it. Here is a way where I tried to make it according to your way of thinking:

    This is script attached to player object (PlayerObject.cs), that simply updates position and rotation of it in every frame, in two public variables:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerObject : MonoBehaviour
    6. {
    7.     public Vector3 playerObjectPos;
    8.     public Quaternion playerObjectRot;
    9.  
    10.     void Update()
    11.     {
    12.         playerObjectPos = transform.position;
    13.         playerObjectRot = transform.rotation;
    14.     }
    15. }
    And this is script attached to spawner-of-obstacles object in the scene (ObstacleSpawner.cs):

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ObstacleSpawner : MonoBehaviour
    6. {
    7.     public float obstacleSpawnDelay = 1.0f;
    8.  
    9.     private GameObject obstacle;
    10.     private Vector3 obstacleObjectPos;
    11.     private Quaternion obstacleObjectRot;
    12.     private float obstacleSpawnTime = 0.0f;
    13.  
    14.     void Start()
    15.     {
    16.         obstacle = Resources.Load<GameObject>("Obstacle");
    17.  
    18.         obstacleSpawnTime = obstacleSpawnDelay;
    19.     }
    20.     void Update()
    21.     {
    22.         if(obstacleSpawnTime <= 0.0f)
    23.         {
    24.             PlayerObject playerObj =
    25.                 GameObject.Find("Player").GetComponent<PlayerObject>();
    26.             if(playerObj != null)
    27.             {
    28.                 obstacleObjectPos = new Vector3(
    29.                     playerObj.playerObjectPos.x,
    30.                     playerObj.playerObjectPos.y - 50.0f,
    31.                     playerObj.playerObjectPos.z);
    32.                 obstacleObjectRot = playerObj.playerObjectRot;
    33.             }
    34.  
    35.             Instantiate(obstacle, obstacleObjectPos, obstacleObjectRot);
    36.             obstacleSpawnTime = obstacleSpawnDelay;
    37.         }
    38.         else
    39.         {
    40.             obstacleSpawnTime -= Time.deltaTime;
    41.         }
    42.     }
    43. }
    Regarding ObstacleSpawner.cs script, I went with simply finding the player's object in the scene and grabbing its PlayerObject component (PlayerObject.cs script that is attached to it):

    PlayerObject playerObj = GameObject.Find("Player").GetComponent<PlayerObject>();


    From that component (script), I used two of its public variables, playerObjectPos and playerObjectRot, to fill in variables obstacleObjectPos and obstacleObjectRot:

    Code (CSharp):
    1. obstacleObjectPos = new Vector3(
    2.     playerObj.playerObjectPos.x,
    3.     playerObj.playerObjectPos.y - 50.0f,
    4.     playerObj.playerObjectPos.z);
    5. obstacleObjectRot = playerObj.playerObjectRot;
    Which I then used to determinate position and rotation of the obstacle that was going to be spawned next:

    Instantiate(obstacle, obstacleObjectPos, obstacleObjectRot);


    Hope this helps :)
     
    Last edited: May 13, 2019