Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 MousePods · Jul 25, 2012 at 11:56 PM · prefabs

Prefab linking

I have an enemycontroller set up. I made it a prefab and now I cannot put the linked scripts in it. How would I do this, and if it is not possible, what is the proper, professional way to do this?

public YoyoCollider yoyoCollider; public CollectableItemsController collectableItemsController;

Comment
Add comment · Show 2
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 wilco64256 · Jul 26, 2012 at 12:01 AM 0
Share

Can you clarify what you mean by "put the linked scripts in it?" Are you trying to make the scripts a child of the prefab, or just add them to the object as components, or what specifically are you trying to do?

avatar image MousePods · Jul 26, 2012 at 12:09 AM 0
Share

I am trying to add them as components so that I can use them in the enemy script. I have not been using unity long enough to know what to call that...I think its components?

I just say " public ScriptName variableScript " and drop the script in the slot in the inspector. Then I just use it to call public variables from the variableScript.

However, you cannot do this when it is a prefab. So how do you call or get references from other scripts the professional way, not in a more hacky way.

3 Replies

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

Answer by Seth-Bergman · Jul 26, 2012 at 12:09 AM

If you need to initialize your variables at runtime, you can use the Start function:

 var script : SomeScript; // needs to be initialized to object in scene
 var enemy : GameObject;
 
 function Start(){
 enemy = GameObject.Find("Enemy");// finds gameobject in scene with name "Enemy"
 script = GameObject.Find("Enemy").GetComponent(SomeScript); // finds object in scene by name and gets SomeScript attached 
 //or
 script = GameObject.FindWithTag("enemy").GetComponent(SomeScript);//finds with a tag 
 }

for example

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 MousePods · Jul 26, 2012 at 12:13 AM 0
Share

Ok, that is what I thought, but I did not know if that is what everyone else does. It seems a slow way to do it? So is this how the professional game dev's do it?

Thanks!

avatar image
0

Answer by Spice Labs · Sep 03, 2013 at 12:45 PM

You can also click on the prefab and scroll down so that you can see the Add Component button at the bottom. then in the project window go to the script folder and drag the script file to the space just below the Add Component button. That should attach the script to the prefab.

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 BlipB · Mar 16, 2019 at 11:19 AM

For the people out there who has been trying to figure out how to link scripts from prefabs and or non prefabs it's your lucky day.

Exsample:

Lets say you want to grab the color from "PlayerControllerScript" and Put it into the "PopEffectScript"

PlayerControllerScript(Prefab)

 public class PlayerControllerScript : MonoBehaviour
 {
     public Color PlayerColor;
     
     // Start is called before the first frame update
     void Start()
     {
        PlayerColor = Color.red;
     }
 
     // Update is called once per frame
     void Update()
     {
         //Enter your logic here
     }

PopEffectScript(Prefab)

 public class PopEffectScript : MonoBehaviour
 {
     // Start is called before the first frame update
     void Start()
     {
         gameObject.GetComponent<Renderer>().material.color = FindObjectOfType<PlayerControllerScript>().PlayerColor;
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 }


Answer: FindObjectOfType().dosomthing();

Bit of back story:

Ever since I've started using unity and UNet(Mirror) you always had to put your player as a prefab instead of a gameobject this way they can spawn, but this is where the problem begins... but if you wanted variables from the script whats in side of the Gun(Prefab) script and want to place the variables into the Player(Prefab) script its a flipping nightmare of a challege I tried all sorts putting prefabs in side of prefabs you name it I've done it... and you'll end up at a point where its not possible todo anyway more, exsample like controlling the camera script from the Player(prefab) script. SO after weeks of searching I came to nothing and thats where I started bashing my head on the desk and found "FindObjectOfType<>()" from what I tested it works great ever since this works flawlesy. But I hope this helps somone if it does your so welcome! cus for how long this has taken me to find, I've got one strand of hair left....

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

9 People are following this question.

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

Related Questions

Can the animation editor create local rotational data? 3 Answers

Unity Water Help 2 Answers

Joining separated prefabs to one source prefab 0 Answers

Scaling down dynamically created objects 1 Answer

Copy clone in prefab 1 Answer


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