• 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 Vingyard · Sep 25, 2017 at 04:04 AM · c#prefab-instance

Getting and switching prefabs

Hello, I'm making a racing game with power ups... And because I'm fairly new to Unity, I'm finding a few problems when trying to make it uncoupled.

The design is more or less this

UML

I also have a Prefab for each powerup, but I don't know where I should save them and where I should instantiate them. For example, one of these power ups is a missile. However I don't know how I should show my missile's prefab in the scene. I kept reading one has to drag and drop the prefabs to use them but I don't want the car to know every single one of the power ups, but just the one he currently has. It would also be great to not have to call the prefab's path to instantiate it

Does anyone know how I could instantiate them?

EDIT: I marked Thaun_'s idea as the solution because it seemed like a good idea, but in the end what I did was make a ScriptableObject that holds all the powerups and assign it to the car's MonoBehaviour and it works like a charm

Comment
Add comment · Show 3
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 Cornelis-de-Jager · Sep 25, 2017 at 04:12 AM 0
Share

Does your players have to drive over them to use the powerups? How are they spawned in the field the first place? What is the ultimate vision you have

avatar image Vingyard Cornelis-de-Jager · Sep 25, 2017 at 10:10 AM 0
Share

What the players do is drive over a power up box and at the OnTriggerEvent, I do something like this

 void OnTriggerEnter(Collider otherObject)
     {
         if (IsPowerUpBox(otherObject))
         {
             if (CanHaveNewPowerUp())
             {
                 myPowerUp = SpecialPowerBuilder.CreateRandomPower(this);
             }
             RemoveObject(otherObject);
         }
     }

I didn't mention the boxes that give you powerups because I believe this would make the power up boxes independent of the power ups

$$anonymous$$y ultimate vision would be to spawn the powerup boxes at fixed places in the map and after a certain interval once they were taken (already implemented); to assign a random powerup once the box is taken (already implemented); and this is where I'm struggling: to be able to show the powerup in the scene once the powerup key is pressed (which is something that should happen in the PowerUp.Activate(CarController))

avatar image Cuttlas-U · Sep 25, 2017 at 10:58 AM 0
Share

hi; i think this is a lot to cover; ad me in my gmail so i can guide u better; savajjad@gmail.com

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Thaun_ · Sep 25, 2017 at 10:44 AM

Drag the GameObject to the hierarchy in a prefab folder or anywhere.

on the start of the script add public GameObject PreFab;

Drag the gameobject in the hierarchy where the prefab is stored to the script.

To instantiate an object do Instantiate(PreFab);

More info here: https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

Comment
Add comment · Show 5 · 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 Vingyard · Sep 25, 2017 at 11:58 AM 0
Share

The problem in that is that I don't know how to call that prefab. If I don't store every single power up in the car monobehavior by dragging and dropping it there like the guide shows (which I think I shouldn't because it's really messy code), how am I able to call that Prefab?

avatar image Thaun_ · Sep 25, 2017 at 12:02 PM 1
Share

You dont store the prefab in the car, you store the path of the prefab. When you instantiate, you copy the object.

Also, if you would like a list of prefabs, you could just do

 public List<GameObject> powerUps;
 public GameObject currentPowerUp;

 void ExampleSpawnPowerup(int powerInt){
     currentPowerUp = Instantiate(powerUps[powerInt]);
 }

if you want to call the script the gameobject has is to do:

 void ExampleActivate(){
     currentPowerUp.GetComponent<PowerUpScript>().Activate(this);
 }

avatar image Vingyard Thaun_ · Sep 25, 2017 at 12:10 PM 0
Share

Hm, that sort of makes me want to make an empty GameObject with a list of all the powerup prefabs and ask that GameObject to instantiate them. Is that considered "unity-like" or is it too ugly?

avatar image Thaun_ · Sep 25, 2017 at 12:18 PM 1
Share

Yes you could, you could just have 1 GameObject containing all the GameObjects for the powerup.

meaning the code will look like this:

 public GameObject powerUpListObj;
 public GameObject currentPowerup;

 void spawnPowerup(int powerInt){
     currentPowerup = Instantiate(powerUpListObj.transform.GetChild(powerInt));
  }
  void ExampleActivate(){
      currentPowerUp.GetComponent<PowerUpScript>().Activate(this);
 }
avatar image Vingyard Thaun_ · Sep 25, 2017 at 12:24 PM 0
Share

That's exactly what I'll do then. Thank you!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

71 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

An OS design issue: File types associated with their appropriate programs 1 Answer

Instantiated object not spawning with given field values 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