• 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 CinnoMan · Apr 24, 2014 at 02:07 PM · prefabsreferences

Can references be applied to prefabs? Workarounds?

Hi all I have various enemy types set up as prefabs. these enemies have scripts with public variables that I use to store references to other objects in the scene through drag n' drop (e.g. a favTarget variable on a TargetSeeker script). The intention is to keep things as generic as possible for game design flexibility.

a) Now it seems I can't apply these drag n drop references to the original prefab (which could make sense since in other scenes the referenced objects might not exist). Is that true?

b) if it is possible, how do I do that? pressing "Apply" doesn't do it.

c) If it's not possible, how can I store these object references in prefabs in a different way, without hardcoding them, so my various scripts retain their flexibility?

thanks for any advice and pointers!

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

2 Replies

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

Answer by Loius · Apr 24, 2014 at 02:15 PM

You can't store references to scene objects in prefabs. You can only store references to "assets" (prefabs, textures, anything in the project view). Usually you create both objects as prefabs and then drag both into the scene; this causes them to link to each other in the scene.

So if you have a Box that wants to know about a Ball, you create a Ball prefab and you create a Box prefab, you drag the Ball prefab into the Box prefab, and then you drag both prefabs into the scene at the same time.

Comment
Add comment · Show 2 · 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 CinnoMan · Apr 25, 2014 at 10:31 AM 0
Share

Thanks for your answer, it makes total sense. It's not quite solving my problems, though;

-In my game, I have (many) enemies pooled and spawn them at run-time, which makes it impossible to manually drag them into the scene along with the other objects they need to know about. It also doesn't seem like an effective workflow in my specific case.

-What alternatives are there? I'm thinking I could make some objects be static so they can just be referenced by all, but it seems a bit of an awkward approach.

to clarify, 2 specific examples of what I want to achieve:

  • target-seeker script for my enemies; I can configure in the editor if they target PlayerA, PlayerB or some other pre-deter$$anonymous$$ed object in the scene

-Everything is rhythm-synced, so some enemy scripts subscribe to one of a variety of pulses (the list of pulses might change). Ideally, I could configure the enemy prefabs in the editor flexibly, selecting a specific pulse from a dropdown list or something like that.

avatar image Loius · Apr 27, 2014 at 12:47 AM 1
Share

Especially for enemies knowing about players, you need to have them dynamically learn about the player. There's very rarely a situation that would benefit from a prefab linked to a scene object.

Ins$$anonymous$$d, you'd have some ability to look up players so that anything can find them:

 public class Player : $$anonymous$$onoBehaviour {
   public static Player[] players = new Player[0];
 
   void Awake() {
     Player[] newList = new Player[players.Length+1];
     for(int i=0; i<players.length; i++) {
       newList[i] = players[i];
     }
     newList[players.Length] = this;
   }
 }
 ...
 public class Enemy : $$anonymous$$onoBehaviour {
   void Start() {
     myTarget = Player.players[Random.Range(0,Player.players.Length)];
   }
 }

Other options include using GameObject.Find and its variants, but to be honest I have not once used any of those because I like the type safety and the inability to mistype a string that I get from using statics or custom-built Find derivatives.

avatar image
1

Answer by Lazarus-Overman · Sep 01, 2016 at 08:31 AM

For anyone still searching for an answer to this problem (i.e., an enemy prefab that must reference the player in order to chase the player), I found that a simple line of code on the prefab enemy solves the problem:

1) Leave the enemy's editor reference slot to "player" empty (as it will be any time that enemy is instantiated).

2) As most enemy follow scripts use "public Transform player" at the very start to tell the enemy what point to follow (thereby creating the empty reference slot in the editor which is usually filled manually with a drag and drop), you just need to add this line to the "void Start () {" function: "player = GameObject.FindGameObjectWithTag ("whatever your tag is").GetComponent();".

This works every time for me and can easily be altered slightly to fit most any situation. Try it and you'll see the empty slot magically fill with the necessary reference upon start.

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 Lazarus-Overman · Aug 31, 2016 at 03:01 PM 0
Share

goes after GetComponent and before ();

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

22 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

Related Questions

How do I keep references to instantiates prefabs? 2 Answers

Prefab doesn't keep it's public variable when Instantiated? 2 Answers

Strip model with submeshes to prefabs or not? 0 Answers

Injecting data into Prefabs 1 Answer

Prefab Spawning With SpawnPoints (Loot System) 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