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

Question Automate something that was hard coded

Discussion in 'Scripting' started by Programer_YEAA, Aug 11, 2022.

  1. Programer_YEAA

    Programer_YEAA

    Joined:
    Jun 3, 2022
    Posts:
    91
    I have the following
    Code (CSharp):
    1. public class Hand : MonoBehaviour
    2. {
    3.     public string Element; // An another class sets this value to "water" or "fire"
    4. }
    5.  
    6. public class CreateElement : MonoBehaviour
    7. {
    8.     Hand hand;
    9.  
    10.     private GameObject waterPrefab
    11.     private GameObject firePrefab
    12.  
    13.     private void Start()
    14.     {
    15.         hand = GetComponent<Hand>();
    16.     }
    17.  
    18.     private void Update()
    19.     {
    20.         //Check what element the programm need to make and then does so
    21.         if (hand.Element == "water") { MakeElement(waterPrefab); }
    22.         if (hand.Element == "fire") { MakeElement(firePrefab); }
    23.     }
    24.  
    25.     private void MakeElement(GameObject element)
    26.     {
    27.         //make the element
    28.     }
    29.  
    30. }
    As you can see I have to hard code every case. But isn't there a way to do this automatic? So if I want to add air to my game. I don't have to change the script?
     
    Last edited: Aug 11, 2022
  2. sisenikelet

    sisenikelet

    Joined:
    Jan 19, 2020
    Posts:
    2
    I would say you should look into "Resources.Load". If you create a new folder called "Resources" (it has to be called that), and put your prefabs in there, you can call:
    Instantiate(Resources.Load([prefabname]))
    to instantiate the prefab that is in the Resources folder.
    If you want more information, just look at the Resources.Load documentation page:
    Unity - Scripting API: Resources.Load (unity3d.com)
     
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,015
  4. Programer_YEAA

    Programer_YEAA

    Joined:
    Jun 3, 2022
    Posts:
    91
    Thx you :)
     
  5. Programer_YEAA

    Programer_YEAA

    Joined:
    Jun 3, 2022
    Posts:
    91
  6. Kreshi

    Kreshi

    Joined:
    Jan 12, 2015
    Posts:
    434
    You can try something like this:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CreateElement : MonoBehaviour
    6. {
    7.     Element element;
    8.  
    9.     private void Start()
    10.     {
    11.         element = GetComponent<Element>();
    12.     }
    13.  
    14.     private void Update()
    15.     {
    16.         element.MakeElement();
    17.     }
    18. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public abstract class Element : MonoBehaviour
    6. {
    7.     public GameObject Prefab;
    8.  
    9.     public virtual void MakeElement()
    10.     {
    11.         Debug.Log("Base make element logic");
    12.     }
    13. }
    14.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FireElement : Element
    6. {
    7.     public override void MakeElement()
    8.     {
    9.         base.MakeElement();
    10.         Debug.Log("Additional logic for fire element");
    11.     }
    12. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class WaterElement : Element
    6. {
    7.     public override void MakeElement()
    8.     {
    9.         base.MakeElement();
    10.         Debug.Log("Additional logic for water element");
    11.     }
    12. }
    13.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AirElement : Element
    6. {
    7.     public override void MakeElement()
    8.     {
    9.         base.MakeElement();
    10.         Debug.Log("Additional logic for air element");
    11.     }
    12. }
    13.  
     
  7. Programer_YEAA

    Programer_YEAA

    Joined:
    Jun 3, 2022
    Posts:
    91
    yeah, I will try that, thx you :)