How to AddForce to a Rigidbody from Game Objects Spawned from an Array, One Prefab at a Time?
Hello.
As the question say, I need to add force to a prefabs that are spawned from an array, and they need to be added to the prefab one at a time.
Let's say there's an array of 10 prefabs that are spawned and moving around. The next time the user clicks on the mouse, AddForce should be added to element 0 in the array, then the next time the mouse button is clicked AddForce should be added to element 1 in the array, so on and so forth. This should go all they way through until the end of the array.
What's a good way to go about doing this?
I initially thought that just adding a public bool and flipping that when the mouse button is clicked, but of course this makes it so all elements in the array are affected by the AddForce.
How can I do it so only one prefab is affected on at a time?
Thanks!
Answer by wibble82 · Dec 15, 2015 at 01:25 PM
Hi
I think you've got your terminology a bit confused. A prefab is a file in unity that acts as a 'template' for an object you can spawn in the world. I think what you're saying is:
I have spawned 10 game objects (presumably by instantiating them from prefabs), and stored references to them in an array
When the mouse button is first clicked, I want to apply a force to object 0 in the array
When it is clicked again, I want to apply a force to object 1
etc etc
I suspect what you're after is something along these lines:
GameObject[] spawned_objects;
int next_object_idx;
void Start()
{
//presumably you spawn objects here using Instantiate and store them in the spawned_objects array?
next_object_idx = 0;
}
void Update()
{
if(Input.GetMouseButtonDown(0))
{
//apply a force
spawned_objects[next_object_idx].rigidBody.AddForce(bla);
//move onto the next object
next_object_idx++;
//if we've gone past the end of the array, wrap it back around to 0
if(next_object_idx > spawned_objects.Length)
next_object_idx = 0;
}
}
Hey, yes, I stand corrected, game objects, not prefabs. Thank you.
I should have clarified, AddForce needs to be done in the FixedUpdate as the spawned game objects need to keep moving over time.
I have a separate function on another script to AddForce that is called when the mouse button is clicked, which works, but again, it needs to be FixedUpdate (of if there is another way to get the same result) so that the movement continues.
Any ideas?
Thanks.
Ok I'm with you I think. So you want clicking the mouse to change which object is currently being affected, but you want to apply the force every frame to the object. In that case something along the lines of:
GameObject[] spawned_objects;
int next_object_idx;
void Start()
{
//presumably you spawn objects here using Instantiate and store them in the spawned_objects array?
next_object_idx = 0;
}
void Update()
{
//in update we just handle detecting a mouse click to change the object we're working with
if(Input.Get$$anonymous$$ouseButtonDown(0))
{
//move onto the next object
next_object_idx++;
//if we've gone past the end of the array, wrap it back around to 0
if(next_object_idx > spawned_objects.Length)
next_object_idx = 0;
}
}
void FixedUpdate()
{
//in fixed update we apply an actual force
spawned_objects[next_object_idx].rigidBody.AddForce(bla);
}
Hope that helps :)
p.s. if an answer is correct - mark it as correct - it means other people with similar questions find it more easily! Obviously if it's not correct, don't :)
Hey,
So the problem I have now is with this line of here:
spawned_objects[next_object_idx].rigidBody.AddForce(bla);
This script is placed on a control manager and I need to access the rigidbody on the spawned gameobjects.
I'm not really sure how to do this. How do you go about doing that?
Thanks!
Answer by keraj37 · Dec 15, 2015 at 02:46 PM
Very simply:
int index = 0;
GameObject[] objects = new GameObject[10] {ob1, ob2....obj10};
public void MouseClicked()
{
index++;
if (index >= 10)
index = 0;
objects [index].GetComponent<Rigidbody> ().AddForce (Vector3.up);
}
So use indexer and increment it when mouse is cliked.
I'm getting the following error:
error CS0021: Cannot apply indexing with [] to an expression of type `UnityEngine.GameObject'
Any ideas?
Your answer
Follow this Question
Related Questions
Rigidbody AddForce() stops working after walking away for a while 0 Answers
Script makes plane point in general direction but doesn't point fully... read description please! 1 Answer
GetAxis being missed in FixedUpdate work around? 1 Answer
AddForce in direction of different object? 1 Answer
Using OnTrigger to make two trigger objects open a door in unityscript. 0 Answers