- Home /
Accessing components via script and assigning randomized values to their parameters?
I have many fixed joints in my scene, on level Load, I would like to assign random values (within a fixed range) to the 'Break Force' and 'Break Torque' Variables of each of these fixed joint components.
Is this possible? Can anybody point me in the direction of figuring out how to do this, or at least let me know if it's doable?
Answer by Positive7 · Jul 30, 2015 at 10:32 AM
using UnityEngine;
using System.Collections.Generic;
public class FixedJointA : MonoBehaviour {
public List<FixedJoint> joints = new List<FixedJoint>();
float forceMin = 0.0f;
float forceMax = 1.0f;
float torqueMin = 0.0f;
float torqueMax = 1.0f;
void Start(){
foreach(FixedJoint i in GetComponents<FixedJoint>()){
joints.Add(i);
}
}
void SetFixedJoint(){
for (int i = 0; i < joints.Count; i++)
{
if(joints[i] != null){
joints[i].breakForce = Random.Range(forceMin, forceMax);
joints[i].breakTorque = Random.Range(torqueMin, torqueMax);
}
}
for(var i = joints.Count - 1; i > -1; i--)
{
if (joints[i] == null)
joints.RemoveAt(i);
}
}
void Update(){
SetFixedJoint();
}
}
$$anonymous$$ade a few tweaks to the code. $$anonymous$$akes no sense to have the FixedJoint array "joints" public. I also moved the GetComponents method to the Awake() method... same there. Don't think there is any good reason to put it in Start().
using UnityEngine;
using System.Collections;
public class Fixedjoint : $$anonymous$$onoBehaviour {
FixedJoint[] joints;
float force$$anonymous$$in = 0.0f;
float force$$anonymous$$ax = 1.0f;
float torque$$anonymous$$in = 0.0f;
float torque$$anonymous$$ax = 1.0f;
void Awake(){
joints = GetComponents<FixedJoint>();
}
// Call this when you want to set the random values. In Start() as an example.
void SetFixedJoint(){
for (int i = 0; i < joints.Length; i++)
{
joints[i].breakForce = Random.Range(force$$anonymous$$in, force$$anonymous$$ax);
joints[i].breakTorque = Random.Range(torque$$anonymous$$in, torque$$anonymous$$ax);
}
}
}
It's public so he can check if it found all the fixed joint he wanted. And I put in start cause I was lazy to rename it :P
Holy God, that's awesome, you guys are great, Do I just put this script on each asset with joints?
and I think I understand the rest of it, although, just a follow up question: Is this the most efficient way of doing this? Is this script particularly expensive performance wise if there are many fixed joints in the scene? (there are hundreds)
Thank's a million lads I really appreciate it, I should be able to test it within an hour or two.
Yeah just put it in parent object and if you have Fixed joints in children too you can use GetComponentsInChildren<Fixedjoint>();
As for performance I have no idea. I never did anything similar to this. let us know :D
This works amazingly guys thanks! no noticeable bump in performance, although it does fill my console with a load of errors after a joint breaks because the script keeps trying to access it it.
Your answer
Follow this Question
Related Questions
Random time interval 1 Answer
setting double in editor vs setting it in script (the latter doesn't work) 1 Answer
How to share a script between multiple gameobjects but with different values 3 Answers
Why is instantiate and playerprefs not working when combined? 1 Answer
updating a gameobject variable from another script attached to another object 3 Answers