Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How do I combine these scripts? (Unity 2D)

Discussion in 'Getting Started' started by Gummi_Ghost, Nov 12, 2021.

  1. Gummi_Ghost

    Gummi_Ghost

    Joined:
    Sep 21, 2020
    Posts:
    35
    I completed Lost Relic Game's video on cloud spawning
    , where he separates the code into two scripts. I tried to combine them into a new script called "SkyObjectMovement", but one script references the other, and I can't make it work.

    If I comment out
    Code (CSharp):
    1. //cloud.GetComponent<CloudScript>().StartFloating(speed, endPoint.transform.position.x);
    , the clouds spawn, but do not move. If I keep the code, it says there's a NullReferenceException. I can't get it to work without referenecing CloudScript, even though it already has the code it wants.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SkyObjectMovement : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     GameObject[] clouds;
    9.  
    10.     [SerializeField]
    11.     float maxSpawnSpeed;
    12.  
    13.     [SerializeField]
    14.     GameObject endPoint;
    15.  
    16.     Vector3 startPos;
    17.  
    18.     private float _speed;
    19.     private float _endPosX;
    20.  
    21.  
    22.     // Start is called before the first frame update
    23.     void Start()
    24.     {
    25.         startPos = transform.position;
    26.         Invoke("AttemptSpawn", UnityEngine.Random.Range(0f, maxSpawnSpeed));
    27.  
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update()
    32.     {
    33.         transform.Translate(Vector3.right * (Time.deltaTime * _speed));
    34.  
    35.         if (transform.position.x > _endPosX)
    36.         {
    37.             Destroy(gameObject);
    38.         }
    39.     }
    40.  
    41.     void SpawnCloud(Vector3 startPos)
    42.     {
    43.  
    44.         int randomIndex = UnityEngine.Random.Range(0, clouds.Length);
    45.         GameObject cloud = Instantiate(clouds[randomIndex]);
    46.  
    47.         float startY = UnityEngine.Random.Range(startPos.y - 1f, startPos.y + 1f);
    48.  
    49.         cloud.transform.position = new Vector3(startPos.x, startY, startPos.z);
    50.  
    51.         float scale = UnityEngine.Random.Range(0.8f, 1.2f);
    52.         cloud.transform.localScale = new Vector2(scale, scale);
    53.  
    54.         float speed = UnityEngine.Random.Range(0.5f, 1.5f);
    55.         //cloud.GetComponent<CloudScript>().StartFloating(speed, endPoint.transform.position.x);
    56.  
    57.  
    58.     }
    59.  
    60.     void AttemptSpawn()
    61.     {
    62.         //check some things.
    63.         SpawnCloud(startPos);
    64.  
    65.         Invoke("AttemptSpawn", UnityEngine.Random.Range(0f, maxSpawnSpeed));
    66.         Debug.Log("A cloud has been made!");
    67.     }
    68.  
    69.  
    70.     public void StartFloating(float speed, float endPosX)
    71.     {
    72.         _speed = speed;
    73.         _endPosX = endPosX;
    74.     }
    75. }
    The CloudScript it is trying to reference is below. It's such a short script. How do I combine it? Any help is appreciated.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CloudScript : MonoBehaviour
    6. {
    7.     private float _speed;
    8.     private float _endPosX;
    9.  
    10.     public void StartFloating(float speed, float endPosX)
    11.     {
    12.         _speed = speed;
    13.         _endPosX = endPosX;
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         transform.Translate(Vector3.right * (Time.deltaTime * _speed));
    20.  
    21.         if(transform.position.x > _endPosX)
    22.         {
    23.             Destroy(gameObject);
    24.         }
    25.     }
    26. }
    27.