Home > Software engineering >  Moving a Prefab in Unity
Moving a Prefab in Unity

Time:11-22

The purpose of the code is to

  1. make a prefab of "Normal" and "Virus"
  2. make them move in random directions
  3. when they collide, change "Normal" into "Virus" Prefabs

However, I've got stucked on step 2. I successfuly made "Normal" and "Virus" Prefabs get spawned at random places. Btw, I have no idea what I should do to supply transform function to Prefabs.

Also, what codes should I use to replace "Normal" Prefabs into "Virus" Prefabs if they collide each other?

These are the codes and pics I used

using UnityEngine;
public class VirusSpawner : MonoBehaviour
{
    [SerializeField]
    private int objectSpawnCount = 5;
    [SerializeField]
    private GameObject[] prefabArray;

    private void Awake()
    {
        for (int i = 0; i < objectSpawnCount;   i)
        {
            int index = Random.Range(0, prefabArray.Length);
            float x = Random.Range(-3, 3);
            float y = Random.Range(-4, 4);
            Vector3 position = new Vector3(x, y, 0);

            Instantiate(prefabArray[index], position, Quaternion.identity);
        }

    }
}

enter image description here

CodePudding user response:

As I understood what you need is a script for your objects, responsible for moving them and controlling all this "Normal" and "Virus" states.

2.1. Create a C# Script (i.e. "Virus") that moves the object as soon as it exists.

2.2. Right after instantiating your prefabs add this script to it:

GameObject newGO = Instantiate(prefabArray[index], position, Quaternion.identity);
newGO.AddComponent<Virus>();
  1. On the new "Virus" script, add the collision detection and variable that holds the state of Normal or Virus

CodePudding user response:

As @Caio Rocha said you can instantiate new Virus Object and add component to it but there can be one more way which can be faster :

  1. Create a Empty Game Object.

  2. Add both (Virus and Normal) prefabs as children of that prefab and drag and drop to project window to create new Prefab.

  3. Attach this script to Parent:

        public class ParentObject : MonoBehaviour
        {
            public GameObject VirusObject;
            public GameObject NormalObject;
            private bool isVirus; 
    
            private void Awake()
            {
                isVirus = (Random.Range(0, 2) == 1); //Randomly Make an object 
                //virus or normal on instantiation
                if (isVirus)
                {
                    TurnToVirus();
                }
                else
                {
                    TurntoNormal();
                }
            }
    
            private void OnCollisionEnter(Collision other)
            {
                ParentObject coll = 
                other.collider.gameObject.GetComponent<ParentObject>();
                if (coll && coll.isVirus) //if other object is virus
                {
                    TurnToVirus();
                }
            }
    
            private void TurnToVirus()
            {
                VirusObject.SetActive(true);
                NormalObject.SetActive(false);
            }
    
            private void TurntoNormal()
            {
                VirusObject.SetActive(false);
                NormalObject.SetActive(true);
            }
        }
    

Now Inside prefab array move this Parent prefab instead of your own prefabs and it will randomly create normal and virus objects and when they interact normal turn to viruses. Make sure this script and collider is now on parent. This is much optimized over adding component individually just for single check.

  • Related