- Home /
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;
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?
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.
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
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!
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.
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....
Your answer
Follow this Question
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