Search code examples
c#unity-game-enginegame-developmentspawning

How can I make an instance of the same Prefab behave differently from other instances in my game?


I'm currently developing a game in Unity and I had a question. When spawning a new clone of a prefab that I created I want the instantiated game objects to move to the position that another gameobject yad at the time of their spawning. This means that by design I need to make different instances of the same gameobject/prefab behave differently or move to different positions even if they are active at the same time. However when going trough the options I have to go about this in my head I only get to extremely complex and very resource intensive solutions (For example creating a dictionary of Lists with each List holding the gameobjects created during it's creation and then making the velocity variable behave differently for different lists ext... ). However I feel like there must be a much simpler and less resource intensive solution since I see this type of problem being solved in games all the time. Does anyone know what I could do?

This is the code responsible for moving the gameobject instances

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InteractMovement : MonoBehaviour
{

    Rigidbody2D rb;
    GameObject target;
    float moveSpeed;
    Vector3 directionToTarget;
    Renderer m_Renderer;

    void Start()
    {
        if (ScoreScript.scoreValue > 4)
        {
            target = GameObject.Find("White Ball");
            rb = GetComponent<Rigidbody2D>();
            moveSpeed = 5f; //Movement speed of all the obstacles and powerups
            InvokeRepeating("MoveInteract", 0f, 1f);
        }
    }

    void MoveInteract() //Method responsable for the movement of the obstacles and stars
    {
        if ( this.gameObject != null)
        {

            directionToTarget = (target.transform.position - transform.position).normalized;
            rb.velocity = new Vector2(directionToTarget.x * moveSpeed,
                                        directionToTarget.y * moveSpeed);


        }
        else
            rb.velocity = Vector3.zero;

    }




}

THis is the code respobsible for creating the instances of the gameobject in question:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class BallSpawnerControl : MonoBehaviour
{

    public Transform[] spawnPoints;
    public GameObject[] interact;
    int randomSpawnPoint, Interact;
    int index = 1;
    public static bool spawnAllowed;

    // Use this for initialization
    void Start()
    {
        spawnAllowed = true;
        InvokeRepeating("SpawnAInteract", 0f, 1f);
    }

    void SpawnAInteract()
    {

        if (spawnAllowed)
        {

            if (index % 5 != 0)
            {
                randomSpawnPoint = Random.Range(0, spawnPoints.Length);
                Interact = 1;
Instantiate(interact[Interact], spawnPoints[randomSpawnPoint].position,
                    Quaternion.identity);
                index++;
            }
            else
            { 
            randomSpawnPoint = Random.Range(0, spawnPoints.Length);
              Interact = 0;
Instantiate(interact[Interact], spawnPoints[randomSpawnPoint].position,
                   Quaternion.identity);
                index++;
             }
        }

    }

}

The Ball and star (Gameobjects that follow) should move towards the position the whiteball held when they were created instead changing their direction towards the whiteball new invokation of MoveInteract


Solution

  • Try storing its the target's position in a Vector3 variable in start

    Vector3 position;
    
    void Start()
    {
        if (ScoreScript.scoreValue > 4)
        {
            target = GameObject.Find("White Ball");
            rb = GetComponent<Rigidbody2D>();
            position = target.transform.position;
            moveSpeed = 5f; //Movement speed of all the obstacles and powerups
            InvokeRepeating("MoveInteract", 0f, 1f);
        }
    }
    
    
    void MoveInteract() //Method responsable for the movement of the obstacles and stars
    {
        if ( this.gameObject != null)
        {
    
            directionToTarget = (position - transform.position).normalized;
            rb.velocity = new Vector2(directionToTarget.x * moveSpeed,
                                        directionToTarget.y * moveSpeed);
    
    
        }
        else
            rb.velocity = Vector3.zero;
    
    }