- Home /
When I Instantiate a prefab with a button it flies away?
When I click on the button it Instatiates the prefab but after that the prefab just flies away in a random direction.
Help would be appreciated. This is my script for the button and the turret.
Button: public GameObject Turret; public GameObject ObjectSpawnPoint;
public void AddObject() {
Instantiate(Turret, ObjectSpawnPoint.transform.position, Quaternion.identity);
}
Controller: RaycastHit hit; Ray ray;
// Update is called once per frame
void Update () {
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, Mathf.Infinity)) {
transform.position = hit.point;
}
}
private void OnCollisionEnter(Collision collision) {
if (collision.collider.gameObject) {
Debug.Log(gameObject.name);
}
}
Answer by Pinkuboxu · Jan 23, 2018 at 06:24 PM
Without seeing your component setup my best guess is if the turret is setup with a rigid body and collider, it may be bouncing off a collider it's instantiating on. Maybe lock the rigid body's xyz position if it's indeed a turret and doesn't move?
Already tried that doesn't work. As it is also a constant force that is being applied and it doesnt fall down or stop.
What is it exactly you are trying to accomplish? I'll have to assume that you are changing the transform.position to follow where the mouse is pointing for placement of the turret object. Try turning the kinematic option on until you click where the turret where it needs to be, then turn kinematic off. If it's still moving when kinematic is on then no force is involved and something else is directly changing the transform.position. I cannot tell what that might be though, unless there's something wrong with hit.point, which seems right to me.
Your answer
Follow this Question
Related Questions
How do you change the int of an object in the scene with an instantiated button? 1 Answer
Is there a way to Instantiate a button prefab as a child of a Canvas? 2 Answers
Instantiated Object being auto-deleted 0 Answers
New GUI and Inventory problem. 1 Answer
Issue Instantiating prefab in C# 0 Answers