- Home /
PreFabs, How do they work? Why the object not longer responds to what the script says?
Thanks to the user MelvMay i got this script working ... really simple stuff ... but what i am concerned is that in the exact moment i turned my "Ball" object to be a prefab this script was no longer working! Why is that? How to fix it? I know it is explained somewhere on the API or Manual ... But i am not an english native speaker and finding Very-Specific things like this is sometimes difficult! Thanks for any help!!
using UnityEngine;
using System.Collections;
public class InfoHolder : MonoBehaviour {
public GameObject ball;
void OnCollisionEnter2D(Collision2D collision){
if(collision.gameObject == ball){
Debug.Log (ball.GetComponent<Rigidbody2D>().velocity);
}
}
}
Answer by aditya · Apr 15, 2016 at 05:36 AM
if you are dragging and dropping ball
after making and putting prefab in scene then it should work but if your dragging and dropping and then making the prefab then this will not work then you had to work like below
void Start(){
ball = GameObject.FindObjectWithTag("YOUR TAG HERE");
}
I knew about FindingObjectsWithTags ... But i never thought of that ... Any explanation on why this happens? ... Or why is required? =)
i used FindObjectWithTag just for the sake of example coz i wanna tell here that after making a prefab your scripts will not use your drag drops which you used before making the game object this script is attached to a prefab and this is not a bug but this is how prefabs are programmed by Unity itself, but if you still want to use drag drop then simply put your prefab in scene and THEN drag drop your items in scripts
Your answer
Follow this Question
Related Questions
How to render a particle system with a preset particle system in a prefab? 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to make a crouch button for mobile? 2 Answers