How can I set a Rigidbody component settings before adding the component to all the objects?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GenerateStairs : MonoBehaviour
{
public GameObject stairsPrefab;
public float delay = 3;
public int stairsNumber = 5;
public Vector3 stairsStartPosition;
public Vector3 stairSize;
public Vector3 stairsSize;
public float stepWidthFactor = 1f;
public MoveObjects moveobjects;
public bool addRigidbody = false;
private Vector3 stairsPosition;
private GameObject stairsParent;
// Use this for initialization
void Start()
{
stairsParent = GameObject.Find("Stairs");
StartCoroutine(BuildStairs());
}
// Update is called once per frame
void Update()
{
}
private IEnumerator BuildStairs()
{
for (int i = 1; i <= stairsNumber; i++)
{
stairsPosition = new Vector3(
stairsStartPosition.x,
stairsStartPosition.y + (i * stairsSize.y),
stairsStartPosition.z + (i * stairsSize.y) * stepWidthFactor);
GameObject stairs = Instantiate(
stairsPrefab,
stairsPosition,
Quaternion.identity);
if(addRigidbody)
{
stairs.AddComponent<Rigidbody>();
}
stairs.tag = "Stair";
stairs.transform.parent = stairsParent.transform;
stairs.transform.localScale = stairSize;
yield return new WaitForSeconds(delay);
}
moveobjects.Init();
}
}
Before adding the rigidbody to each stair here:
stairs.AddComponent<Rigidbody>();
I want first in the Start to set the rigidbody values settings using public variables like mass drag use gravity and other stuff of rigidbody then to add to each object the rigidbody with already the settings I did.
No if I'm adding the rigidbody to each object it will be with the default rigidbody settings I want to control this settings before adding the rigidbody.
c# unity3d
add a comment |
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GenerateStairs : MonoBehaviour
{
public GameObject stairsPrefab;
public float delay = 3;
public int stairsNumber = 5;
public Vector3 stairsStartPosition;
public Vector3 stairSize;
public Vector3 stairsSize;
public float stepWidthFactor = 1f;
public MoveObjects moveobjects;
public bool addRigidbody = false;
private Vector3 stairsPosition;
private GameObject stairsParent;
// Use this for initialization
void Start()
{
stairsParent = GameObject.Find("Stairs");
StartCoroutine(BuildStairs());
}
// Update is called once per frame
void Update()
{
}
private IEnumerator BuildStairs()
{
for (int i = 1; i <= stairsNumber; i++)
{
stairsPosition = new Vector3(
stairsStartPosition.x,
stairsStartPosition.y + (i * stairsSize.y),
stairsStartPosition.z + (i * stairsSize.y) * stepWidthFactor);
GameObject stairs = Instantiate(
stairsPrefab,
stairsPosition,
Quaternion.identity);
if(addRigidbody)
{
stairs.AddComponent<Rigidbody>();
}
stairs.tag = "Stair";
stairs.transform.parent = stairsParent.transform;
stairs.transform.localScale = stairSize;
yield return new WaitForSeconds(delay);
}
moveobjects.Init();
}
}
Before adding the rigidbody to each stair here:
stairs.AddComponent<Rigidbody>();
I want first in the Start to set the rigidbody values settings using public variables like mass drag use gravity and other stuff of rigidbody then to add to each object the rigidbody with already the settings I did.
No if I'm adding the rigidbody to each object it will be with the default rigidbody settings I want to control this settings before adding the rigidbody.
c# unity3d
add a comment |
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GenerateStairs : MonoBehaviour
{
public GameObject stairsPrefab;
public float delay = 3;
public int stairsNumber = 5;
public Vector3 stairsStartPosition;
public Vector3 stairSize;
public Vector3 stairsSize;
public float stepWidthFactor = 1f;
public MoveObjects moveobjects;
public bool addRigidbody = false;
private Vector3 stairsPosition;
private GameObject stairsParent;
// Use this for initialization
void Start()
{
stairsParent = GameObject.Find("Stairs");
StartCoroutine(BuildStairs());
}
// Update is called once per frame
void Update()
{
}
private IEnumerator BuildStairs()
{
for (int i = 1; i <= stairsNumber; i++)
{
stairsPosition = new Vector3(
stairsStartPosition.x,
stairsStartPosition.y + (i * stairsSize.y),
stairsStartPosition.z + (i * stairsSize.y) * stepWidthFactor);
GameObject stairs = Instantiate(
stairsPrefab,
stairsPosition,
Quaternion.identity);
if(addRigidbody)
{
stairs.AddComponent<Rigidbody>();
}
stairs.tag = "Stair";
stairs.transform.parent = stairsParent.transform;
stairs.transform.localScale = stairSize;
yield return new WaitForSeconds(delay);
}
moveobjects.Init();
}
}
Before adding the rigidbody to each stair here:
stairs.AddComponent<Rigidbody>();
I want first in the Start to set the rigidbody values settings using public variables like mass drag use gravity and other stuff of rigidbody then to add to each object the rigidbody with already the settings I did.
No if I'm adding the rigidbody to each object it will be with the default rigidbody settings I want to control this settings before adding the rigidbody.
c# unity3d
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GenerateStairs : MonoBehaviour
{
public GameObject stairsPrefab;
public float delay = 3;
public int stairsNumber = 5;
public Vector3 stairsStartPosition;
public Vector3 stairSize;
public Vector3 stairsSize;
public float stepWidthFactor = 1f;
public MoveObjects moveobjects;
public bool addRigidbody = false;
private Vector3 stairsPosition;
private GameObject stairsParent;
// Use this for initialization
void Start()
{
stairsParent = GameObject.Find("Stairs");
StartCoroutine(BuildStairs());
}
// Update is called once per frame
void Update()
{
}
private IEnumerator BuildStairs()
{
for (int i = 1; i <= stairsNumber; i++)
{
stairsPosition = new Vector3(
stairsStartPosition.x,
stairsStartPosition.y + (i * stairsSize.y),
stairsStartPosition.z + (i * stairsSize.y) * stepWidthFactor);
GameObject stairs = Instantiate(
stairsPrefab,
stairsPosition,
Quaternion.identity);
if(addRigidbody)
{
stairs.AddComponent<Rigidbody>();
}
stairs.tag = "Stair";
stairs.transform.parent = stairsParent.transform;
stairs.transform.localScale = stairSize;
yield return new WaitForSeconds(delay);
}
moveobjects.Init();
}
}
Before adding the rigidbody to each stair here:
stairs.AddComponent<Rigidbody>();
I want first in the Start to set the rigidbody values settings using public variables like mass drag use gravity and other stuff of rigidbody then to add to each object the rigidbody with already the settings I did.
No if I'm adding the rigidbody to each object it will be with the default rigidbody settings I want to control this settings before adding the rigidbody.
c# unity3d
c# unity3d
asked Nov 17 '18 at 20:53


Dubi DuboniDubi Duboni
18319
18319
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I want to control this settings before adding the rigidbody.
You can't because you can't because you cannot add existing component to a GameObject. The AddComponent
function adds new instance of the Component to the GameObject.
Note that the AddComponent
function adds the component then returns the instance it just added to the GameObject. You can modify that instance component it returned and that's what you should do.
The Rigidbody that holds the default settings:
public Rigidbody deafultSettings;
Add the Rigidbody component to your stairs GameObject then return the added component:
Rigidbody stairRb = stairs.AddComponent<Rigidbody>();
You can now modify the returned component or make a function to simplify that for you:
CopyRigidBodySettings(deafultSettings, stairRb);
The CopyRigidBodySettings
function:
void CopyRigidBodySettings(Rigidbody destination, Rigidbody source)
{
destination.useGravity = source.useGravity;
destination.mass = source.mass;
destination.drag = source.drag;
destination.angularDrag = source.angularDrag;
}
Components are modified after adding them not before adding them. That shouldn't be a problem but if you don't like that, use a prefab. In fact, this is what prefabs are made for so that you can create a GameObject with default settings then re-use them. Create a prefab of your stairs with Rigidbody
already attached to them then use the Instantiate
function to instate the prefab. You can modify the prefab in the Editor or script before instantiating it.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53355487%2fhow-can-i-set-a-rigidbody-component-settings-before-adding-the-component-to-all%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I want to control this settings before adding the rigidbody.
You can't because you can't because you cannot add existing component to a GameObject. The AddComponent
function adds new instance of the Component to the GameObject.
Note that the AddComponent
function adds the component then returns the instance it just added to the GameObject. You can modify that instance component it returned and that's what you should do.
The Rigidbody that holds the default settings:
public Rigidbody deafultSettings;
Add the Rigidbody component to your stairs GameObject then return the added component:
Rigidbody stairRb = stairs.AddComponent<Rigidbody>();
You can now modify the returned component or make a function to simplify that for you:
CopyRigidBodySettings(deafultSettings, stairRb);
The CopyRigidBodySettings
function:
void CopyRigidBodySettings(Rigidbody destination, Rigidbody source)
{
destination.useGravity = source.useGravity;
destination.mass = source.mass;
destination.drag = source.drag;
destination.angularDrag = source.angularDrag;
}
Components are modified after adding them not before adding them. That shouldn't be a problem but if you don't like that, use a prefab. In fact, this is what prefabs are made for so that you can create a GameObject with default settings then re-use them. Create a prefab of your stairs with Rigidbody
already attached to them then use the Instantiate
function to instate the prefab. You can modify the prefab in the Editor or script before instantiating it.
add a comment |
I want to control this settings before adding the rigidbody.
You can't because you can't because you cannot add existing component to a GameObject. The AddComponent
function adds new instance of the Component to the GameObject.
Note that the AddComponent
function adds the component then returns the instance it just added to the GameObject. You can modify that instance component it returned and that's what you should do.
The Rigidbody that holds the default settings:
public Rigidbody deafultSettings;
Add the Rigidbody component to your stairs GameObject then return the added component:
Rigidbody stairRb = stairs.AddComponent<Rigidbody>();
You can now modify the returned component or make a function to simplify that for you:
CopyRigidBodySettings(deafultSettings, stairRb);
The CopyRigidBodySettings
function:
void CopyRigidBodySettings(Rigidbody destination, Rigidbody source)
{
destination.useGravity = source.useGravity;
destination.mass = source.mass;
destination.drag = source.drag;
destination.angularDrag = source.angularDrag;
}
Components are modified after adding them not before adding them. That shouldn't be a problem but if you don't like that, use a prefab. In fact, this is what prefabs are made for so that you can create a GameObject with default settings then re-use them. Create a prefab of your stairs with Rigidbody
already attached to them then use the Instantiate
function to instate the prefab. You can modify the prefab in the Editor or script before instantiating it.
add a comment |
I want to control this settings before adding the rigidbody.
You can't because you can't because you cannot add existing component to a GameObject. The AddComponent
function adds new instance of the Component to the GameObject.
Note that the AddComponent
function adds the component then returns the instance it just added to the GameObject. You can modify that instance component it returned and that's what you should do.
The Rigidbody that holds the default settings:
public Rigidbody deafultSettings;
Add the Rigidbody component to your stairs GameObject then return the added component:
Rigidbody stairRb = stairs.AddComponent<Rigidbody>();
You can now modify the returned component or make a function to simplify that for you:
CopyRigidBodySettings(deafultSettings, stairRb);
The CopyRigidBodySettings
function:
void CopyRigidBodySettings(Rigidbody destination, Rigidbody source)
{
destination.useGravity = source.useGravity;
destination.mass = source.mass;
destination.drag = source.drag;
destination.angularDrag = source.angularDrag;
}
Components are modified after adding them not before adding them. That shouldn't be a problem but if you don't like that, use a prefab. In fact, this is what prefabs are made for so that you can create a GameObject with default settings then re-use them. Create a prefab of your stairs with Rigidbody
already attached to them then use the Instantiate
function to instate the prefab. You can modify the prefab in the Editor or script before instantiating it.
I want to control this settings before adding the rigidbody.
You can't because you can't because you cannot add existing component to a GameObject. The AddComponent
function adds new instance of the Component to the GameObject.
Note that the AddComponent
function adds the component then returns the instance it just added to the GameObject. You can modify that instance component it returned and that's what you should do.
The Rigidbody that holds the default settings:
public Rigidbody deafultSettings;
Add the Rigidbody component to your stairs GameObject then return the added component:
Rigidbody stairRb = stairs.AddComponent<Rigidbody>();
You can now modify the returned component or make a function to simplify that for you:
CopyRigidBodySettings(deafultSettings, stairRb);
The CopyRigidBodySettings
function:
void CopyRigidBodySettings(Rigidbody destination, Rigidbody source)
{
destination.useGravity = source.useGravity;
destination.mass = source.mass;
destination.drag = source.drag;
destination.angularDrag = source.angularDrag;
}
Components are modified after adding them not before adding them. That shouldn't be a problem but if you don't like that, use a prefab. In fact, this is what prefabs are made for so that you can create a GameObject with default settings then re-use them. Create a prefab of your stairs with Rigidbody
already attached to them then use the Instantiate
function to instate the prefab. You can modify the prefab in the Editor or script before instantiating it.
answered Nov 17 '18 at 21:24


ProgrammerProgrammer
75.7k1083147
75.7k1083147
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53355487%2fhow-can-i-set-a-rigidbody-component-settings-before-adding-the-component-to-all%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown