- Home /
Unity Prefab reference change
Hello, I have a script that uses a Transform item located in Prefab.
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;
}
}
Answer by Littellittel · Jul 09, 2017 at 09:54 PM
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;
Answer by FirefIy · Jul 11, 2017 at 07:28 PM
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 :(
How many types of attack are you going to have? Just store all of the prefabs as references then you can have anther reference that is the active one.
Answer by Cornelis-de-Jager · Jul 11, 2017 at 09:55 PM
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;
Your answer
![](https://koobas.hobune.stream/wayback/20220612125848im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2D game Unity5 1 Answer
Each click in Unity opens a window with the text “code compilation”. 0 Answers
How can i create a 2D polygon map for my infinte runner game 0 Answers
Timer jitters? SOLVED 0 Answers