I am instantiating a prefab but the attached script on the prefab is not attached during runtime. All other components are on the object but not the script.
the script that instantiates:
public class PointerController : MonoBehaviour
{
public GameObject bullet;
private float AngleRad;
private float AngleDeg;
private Vector3 mPos;
void Start()
{
mPos = new Vector3();
}
// Update is called once per frame
void Update()
{
mPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
// Get Angle in Radians
AngleRad = Mathf.Atan2(this.transform.position.y - mPos.y, this.transform.position.x - mPos.x);
// Get Angle in Degrees
AngleDeg = (180 / Mathf.PI) * AngleRad + 90;
// Rotate Object
//this.transform.rotation = Quaternion.Euler(0, 0, AngleDeg);
this.transform.rotation = Quaternion.Lerp(this.transform.rotation, Quaternion.Euler(0, 0, AngleDeg), .7f);
if (Input.GetMouseButtonDown(0))
{
GameObject go = GameObject.Instantiate(bullet, this.transform.position, this.transform.rotation) as GameObject;
}
}
}
prefab script:
public class BulletBehavior : MonoBehaviour
{
public float spd = 10;
private Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
rb = this.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
rb.velocity = this.transform.forward * spd;
Debug.Log("here");
}
void OnTriggerEnter2D()
{
Destroy(this);
}
}