- Home /
Trying to get 2 gameobject arrays to differ from each other
Hello, I'm having difficulty with trying to separate the 2 arrays so that way they have 2 different distance values. this script is attached to several child game objects & right now they all reach the same distance when im trying to differ the inner & the outter distance game objects. The pop function is being called on the start of another gameobject.
Vector3 v;
public Transform center;
public float speed, radius, radius2;
public GameObject[] inner ,outter;
void Start()
{
v = transform.position - center.position;
}
public void Pop()
{
if (Vector2.Distance(transform.position, v) < radius)
{
for (int i = 0; i < outter.Length; i++)
{
transform.position = Vector2.MoveTowards(transform.position, center.position, -1 * speed);
}
}
if (Vector2.Distance(transform.position, v) < radius2)
{
for (int i = 0; i < inner.Length; i++)
{
transform.position = Vector2.MoveTowards(transform.position, center.position, -1 * speed);
}
}
}
Your question doesnt really tell us anything about your problem or what you are trying to achieve. Can you ins$$anonymous$$d phrase it in that manner as your approach may be inherently flawed and we can help you find a more appropriate one.
Sorry about that my problem is my inner & outter arrays are reaching the same distance when im trying to differ the radius distance for both arrays with the loops for (int i = 0; i < inner.Length; i++)
for (int i = 0; i < outter.Length; i++)
If you want to apply something to each gameObject contained in the inner
and outter
(proper spelling: "outer") arrays, then you need to change transform.position...
to e.g. inner[i].transform.position
in those loops. Also, make sure these arrays are set right at start, and also, if the Pop()
event doesn't happen every frame, consider using dynamic methods to get the objects during runtime, e.g. by giving them components like Inner
and then using GetComponentInChildren<Inner>()
.
hello thank you for your reply sorry about the spelling but when I tried the inner[i].transform.position
it clamped all the game objects together.
Answer by logicandchaos · Feb 04, 2020 at 01:13 AM
Well it's because you are using the same variable to set the distance for each object, speed. there are multiple ways to solve this and which one is better is just preference. It is all about your speed variable, which I can't see defined but assume is a float. First off it is badly named you should call it spawnDistence or something like that variable names should be very descriptive. You could use 2 speed variables, float speed1; float speed2; or you could make it a parameter in your Pop() method, so it would be public void Pop(float speed), which I would choose out of the 2, unless you have to call that function from a button or something. You can also explore other ways to solve this, be creative!
hello, thank you for pointing that out but I tried both methods & it did not solve the issue. the inner gameobjects should not reach the same distance as the outter game objects im not sure where the error is
Your answer
Follow this Question
Related Questions
What's the best way to find the smallest Vector3.Distance of an array of enemies? 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# Creating a 2d boundary 1 Answer