- Home /
Duplicate Question
AddForce not working on Instantiated Object
I had an object that was in my hierarchy and when certain conditions were met, a force (AddForce) was applied. This worked very well until I made a prefab out of the original object… now, the object is instantiated and it just drops till it hits the floor and the AddForce does nothing. I searched and it appears that applying a force to an object that has been instantiated has been problematic yet nothing I have read or tried over the past six hours seems to make any difference.
The object I am attempting to AddForce to is ‘tire_wheel_prefab_2’. Again, before this became a prefab and was instantiated, it worked perfectly. On the ‘tire_wheel_prefab_2 there is a Rigidbody added, ‘Use Gravity’ is checked, and ‘Is Kinematic’ is unchecked. The settings are identical to the object I used before (when it worked) but AddForce is doing nothing. The ‘sphere’ and ‘heart_2’ commands immediately above the AddForce do exactly what they are supposed to so I know that the conditions are being met.
Can someone assist me in getting the AddForce to work properly?
Sincerely,
Daryl G
using Phidgets;
using Phidgets.Events;
using UnityEngine;
using System.Collections;
public class PhidgetsController : MonoBehaviour
{
private InterfaceKit InterfaceKitController = new InterfaceKit();
private int arbitraryCounter = 0;
private int oldarbitraryCounter = 0;
public new ParticleSystem particleSystem;
public GameObject sphere;
public GameObject heart_2;
public Rigidbody tire_wheel_prefab_2;
public GameObject TextHintGUI;
TextHints bPScript;
public bool playsound = false;
void Start()
{
particleSystem.Stop();
Instantiate (tire_wheel_prefab_2, new Vector3(5.6f, 6f, 6f), Quaternion.Euler(270, 0, 0));
}
//Sensor Change event handler...Display sensor index and new value to the console
void InterfaceKit_SensorChange(object sender,SensorChangeEventArgs e)
{
arbitraryCounter = e.Value;
}
void FixedUpdate()
{
if (arbitraryCounter>oldarbitraryCounter) {
particleSystem.Play();
if (playsound==false) {
audio.Play();
playsound=true;
}
bPScript = TextHintGUI.GetComponent <TextHints>();
//if high bp is NOT selected (i.e., normal bp)
if (bPScript.whichBPOption == false) {
sphere.transform.localScale += new Vector3(((arbitraryCounter-500)/50) * Time.deltaTime, ((arbitraryCounter-500)/50) * Time.deltaTime, ((arbitraryCounter-500)/50) * Time.deltaTime);
heart_2.transform.Rotate(Vector3.right, 20 * Time.deltaTime); //this rotates heart when pumping
tire_wheel_prefab_2.AddForce(-30,5,10);
A prefab is like a blueprint. It is used to construct real object. You are applying your force to the prefab, when what you want to do is apply force to the newly created object. Save the return value of the Instantiate in a class instance variable:
tire = Instantiate (tire_wheel_prefab_2, new Vector3(5.6f, 6f, 6f), Quaternion.Euler(270, 0, 0)) as Rigidbody;
Follow this Question
Related Questions
How can I have an access to the rigidbody component of each instantiated object? 1 Answer
[Solved] Storing a GameObject in a var and then instantiating it in a new scene? 2 Answers
Instantiate an instance? 1 Answer
[4.6] Instantiating an object within a ScrollView makes the object invisible 0 Answers
Instantiate script for randomly selecting sound is suddenly creating New Game Object in hierarcy?? 2 Answers