Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Caolan · Jul 30, 2015 at 10:16 AM · variablerandomscriptingbasicscomponentscriptableobject

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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();
     }
 }
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TruffelsAndOranges · Jul 30, 2015 at 10:36 AM 0
Share

$$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);
          }
      }
  }
avatar image Positive7 · Jul 30, 2015 at 10:44 AM 0
Share

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

avatar image Caolan · Jul 30, 2015 at 11:30 AM 0
Share

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.

avatar image Positive7 · Jul 30, 2015 at 11:53 AM 0
Share

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

avatar image Caolan · Aug 04, 2015 at 10:24 AM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges