Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by FirefIy · Jul 09, 2017 at 07:04 PM · transform2d game2d-platformer

Unity Prefab reference change

Hello, I have a script that uses a Transform item located in Prefab. alt text

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

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;
             }
 
         }
 

1.png (28.9 kB)
2.png (31.9 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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;

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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 :(

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jwizard93 · Jul 11, 2017 at 09:12 PM 1
Share

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.

avatar image
0

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;
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

86 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D game Unity5 1 Answer

How do I make something like a conical street light in a 2D side scroller platformer? (Much like that what is used in Mark of the Ninja) 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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges