How To Use 1 Script on Multiple Objects (In My Case AddingForce To Ridgidbodies)
First of all i am a super noob :)
Ok to dumb it down i have a Main menu scene, in this scene i have 1 Menu_Avatar (a simple cube) that spins and when i click the start button my avatar shoots off (using Rigidbody addforce/torque). Now i decided i want 3 objects to do the exact same thing at the same time when i quit the game so i now have 3 Quit_Avatars, I Duplicate my Menu_Avatar_Movement script and Name it Quit_Avatar_Movement making sure to rename all the classes and stuff so its all unique with no Conflicts/Errors.
So i add my script component to all 3 avatars and Individually link each Ridgedbody component to their corresponding script component. Now running the game all Quit_Avatars correctly spin using code within void FixedUpdate But when i use the Quit button or my "debug key" to call Public void QuitForce() Only 1 of the Avatars actually execute the code in the script??
This is my Code for reference:
public class Quit_Avatar_Movement : MonoBehaviour
{
#region References & Values
public Rigidbody Rb;
public float YRoteForce = 200f;
public float ZBlastForce = 3500f;
public float BlastRoteForce = 200f;
public bool IsAvatarFrzn = true;
#endregion
#region Fixed Update
// Update is called once per frame
void FixedUpdate()
{
//Spins Quit_Avatar When its frozen in place
if (IsAvatarFrzn == true)
{
Rb.AddTorque(0, YRoteForce * Time.fixedDeltaTime, 0, ForceMode.VelocityChange);
//Fixed Update seems to work correctly on all Avatars
}
}
#endregion
public void QuitForce() //Is Called Using "FindObjectOfType<Quit_Avatar_Movement>().QuitForce();" When Quit Button Is Pressed
{
Debug.Log("QuitForce Called");
IsAvatarFrzn = false;
Rb.constraints = RigidbodyConstraints.None;
Rb.angularDrag = 0;
Rb.AddForce(0, 0, -ZBlastForce * Time.fixedDeltaTime, ForceMode.VelocityChange);
Rb.AddTorque(-BlastRoteForce * Time.fixedDeltaTime, 0, 0, ForceMode.VelocityChange);
Rb.AddTorque(0, -BlastRoteForce * Time.fixedDeltaTime, 0, ForceMode.VelocityChange);
//This whole "fucntion" (if thats the right term to use) Only affects 1 Avatar?
}
}
Answer by lcplrice · May 21, 2020 at 06:07 AM
With your comment next to QuitForce() method I think you maybe using the incorrect find method.
Instead of FindObjectOfType use FindObjectsOfType (Notice Objects instead of Object).
Try this when quit button is pressed:
var quit_avatars = FindObjectsOfType<Quit_Avatar_Movement>();
foreach (var quit_avatar in quit_avatars)
{
quit_avatar.QuitForce();
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612234729im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can someone please help me find out what wrong with my code. 0 Answers
How do i change a color of an individual alphabet of a UI.Text 2 Answers
How can i look for collisions of the bulidngs in may array ? how can i use OnTRiggerEnter/exit ? 0 Answers
NPC walking on air when targeting player 1 Answer
videoPlayer Script Help Needed 0 Answers