Unity Prefab reference change

Hello,
I have a script that uses a Transform item located in Prefab.
97416-1.png

I can drag one of my 3 prefabs onto that spot to change my player’s attack style

How can I do this through code?
This is how I access Hero Attack script through Player Controller script

 if (weapon.hero_fire.ToString().Contains("Fireball"))
        {
            if ((Input.GetKeyDown(KeyCode.V) || Input.GetMouseButtonDown(1)) && grounded && Math.Abs(playerBody.velocity.x) <= 0.1F)
            {

                if (weapon != null)
                {
                    // false because the player is not an enemy
                    weapon.AttackFire(false);
                }

                attackState = 1;
                barkTimer = barkAnimation;
            }

        }


        if (weapon.hero_fire.ToString().Contains("Firewave"))
        {
            if ((Input.GetKeyDown(KeyCode.V) || Input.GetMouseButtonDown(1)) && grounded && Math.Abs(playerBody.velocity.x) <= 0.1F)
            {
                canWalk = false;
                currentAnimaton = fireWaveCast;
                attackState = 1;
                barkTimer = barkAnimation;
            }

        }

        if (weapon.hero_fire.ToString().Contains("Firebreath"))
        {
            if ((Input.GetKeyDown(KeyCode.V) || Input.GetMouseButtonDown(1)))
            {

                if (weapon != null)
                {
                    // false because the player is not an enemy
                    weapon.AttackFire(false);
                }

                attackState = 2;
                barkTimer = barkAnimation;
            }

        }

In your Player Controller you should get a refrence to the prefabs and then do something like this:

gameobject.GetComponent<Hero Attack>().Hero_fire = myPrefab;

I need to do something like
if (Input.GetKeyDown(KeyCode.Keypad0))
{
weapon.hero_fire = (Transform) @“Assets/Prefabs/Firebreath.prefab”;
}

But I can’t cast Transform to string, sadly :frowning:

Unity has a special inbuilt API for exactly the thing you are looking for. Its called “Resources”. Below is how you do it:

Step 1: Resources Folder

Within your asset folder create a new folder called “Resources”. It has to be the exact spelling.

Step 2: Populate Resource Folder

Simply Drop and drag the file into the Resources folder that you want to reference.

Step 3: Load from Resources

This is the part you want. You can now load any prefabs/resources from your Resources folder by giving the correct path:

Resources.Load ("Firebreath.prefab", typeof (Transform))); 

For the code above, the Actual path is Assets/Resources/Firebreath.prefab. However, we do not specify the Assets/Resources path. The Resources.Load () does that for us.

So if you want to implement it correctly in your script it would look something like this:

// load the Object From Resources
GameObject prefabRef = Resources.Load ("Firebreath.prefab", typeof (GameObject))); 

// Instantiate it
GameObject weapon = Instantiate (prefabRef) as GameObject;

// Assign it to the hero
hero.hero_fire = weapon.transform;