1

I'm creating charged particle simulator in Unity3D and need to apply a function to each object within an array.

The array is defined as

var mcp = new Array(GameObject.FindGameObjectsWithTag("MovingChargedParticle"));

My current function to affect each object within the array is:

function applyMagneticForce(particle1, particle2){

    for (MovingChargedParticle mcp in mcp){

    var NewForce : Vector3.zero;

    var distance=Vector3.Distance(particle1.transform.position, particle2.transform.position);
    var force=1000 * cp.charge * mcp.charge / Mathf.Pow (distance, 2);

    NewForce += force * distance * cycleinterval;    

    }
}

For now, cp.charge and mcp.charge are placeholders for calling a public var on other objects..something else I haven't figured out.

3
  • 1
    You know what .. "unityscript" is totally deprecated. You really have to change to c#. (Fortunately it is extremely easy - it's much easier.) Indeed, simply use List<> in Unity, realistically you never use arrays for any reason. Commented Mar 15, 2016 at 23:01
  • Hey Joe, you pointed that out on an earlier question I posted. Which I do totally appreciate, and will absolutely use that advice for the next project I start. However, I'd like to finish this project out using javascript since I've already completed so much of it.. I'll try using List<> instead. Commented Mar 15, 2016 at 23:06
  • understood; sorry for repeat. you know, it's incredibly difficult to do actual OO stuff in javascript. cheers! Commented Mar 15, 2016 at 23:07

1 Answer 1

1

Something like this:

var objects : GameObject[] = FindObjectsOfType(GameObject) as GameObject[];

for(var item : GameObject in objects)
{
    print (item.transform.position);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.