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

(beginner help?) Change an object's value inside an instantiated prefab's script?

Discussion in 'Prefabs' started by Deku42, Feb 20, 2020.

  1. Deku42

    Deku42

    Joined:
    Feb 5, 2020
    Posts:
    5
    Hello! I've recently begun making my first game in Unity, and have run into an issue with prefabs I don't really understand...
    So, in my project I have an empty object "Spawner" with a "Spawner" script that spawns enemies above the screen at a set interval which then fall, and you need to shoot each one before they land on the player.

    It's code is simple enough:
    Code (CSharp):
    1. private void Update()
    2.     {
    3.         if (respawnDelay <= 0)
    4.         {
    5.             Instantiate(enemies[rand], transform.position, Quaternion.identity);
    6.             respawnDelay = respawnRate;
    7.         }
    8.         else
    9.         {
    10.             respawnDelay -= Time.deltaTime;
    11.         }
    12.     }
    The enemies[] array is filled with the 3 enemy prefabs I put in the editor, which it randomly selects from.

    Attached to each of the 3 enemy object prefabs, I have a "Enemy Projectile" script which moves the enemy object down and reloads the scene if it collides with the player (resetting the game):
    Code (CSharp):
    1. private void FixedUpdate()
    2.     {
    3.         transform.Translate(Vector3.down * enemyProjectileSpeed * Time.deltaTime);
    4.     }
    5.  
    6.     private void OnTriggerEnter2D(Collider2D collision)
    7.     {
    8.         if (collision.CompareTag("Player"))
    9.         {
    10.             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    11.         }
    12.     }

    Now, attached to my Player object, I have a script which detects input from the user and then shoots out 1 of 3 prefab'd bullets, when either A, S, or D is pressed.

    Code:
    Code (CSharp):
    1. private void PlayerShoot()
    2.              if (Input.GetKeyDown(KeyCode.A))
    3.             {
    4.                 Instantiate(projectiles[0], new Vector2(transform.position.x, transform.position.y + 0.6f), Quaternion.identity);
    5.             }
    6.             else if (Input.GetKeyDown(KeyCode.S))
    7.             {
    8.                 Instantiate(projectiles[1], new Vector2(transform.position.x, transform.position.y + 0.6f), Quaternion.identity);
    9.             }
    10.             else if (Input.GetKeyDown(KeyCode.D))
    11.             {
    12.                 Instantiate(projectiles[2], new Vector2(transform.position.x, transform.position.y + 0.6f), Quaternion.identity);
    13.             }
    14.         }
    15.        
    Each of the projectiles the player shoots is chosen from a list of 3 prefab'd bullets and each of these bullet prefabs has a script attached, called "Projectile Script". In this script, the bullet is moved upward starting from the player's position, and destroys the enemy projectile if it collides with it.


    The problem I am having is figuring out how to detect collision between those prefab'd objects I instantiated during runtime. The projectiles my player is shooting, and the projectiles falling from the sky. I can use the OnTriggerEnter2D() method inside the player's bullet objects' scripts to detect collision between itself and an enemy through tag searching, but cannot figure out how to add a reference to anobject outside of the prefab itself.

    I'm trying to get a UI Text object which displays my score in the top left of my screen to go up every time one of my bullets collide, but cannot figure out how to make a reference to it and change its text in the projectile prefab's code. I also can't figure out a way to detect the collision between the objects outside of the prefabs, like in some other script.

    I hope what I asked makes sense! Thank you!
     
    Last edited: Feb 20, 2020
  2. Deku42

    Deku42

    Joined:
    Feb 5, 2020
    Posts:
    5
    For reference, I decided to model my game after Blackthornprod's "MAKING A GAME IN 10 MINUTES IN UNITY - BRACKEYS CHALLENGE !" video, link to video so you can see what my project sorta-looks like (11:55 shows the end result):
     
    Last edited: Feb 20, 2020