I’ve got a long winded setup process, that roles out a bunch of prefabs at the start of gameplay, some of the prefabs have similar, but not exactly the same settings, and sometimes not the same original prefab, that I’d like to change during play testing often to find the desired settings.
Here’s the problem, I’d like to have some little functions that affect all items in the following functions as they create the objects, but I don’t know two things. 1, is this possible? 2, what’s the syntax to have the smaller function inside the larger function affect the individual objects in an array? (UnityScript)
Here’s an effort at a code example of what I’m trying to do…
function Small_Individual_Setting_to_affect_object () {
// some small setting, eg set rigidbody to kinematic and to interpolate physics
}
function Create_Row_One_of_Prefab_of_type_1 () {
// creates a dozen items, 1 at a time with a for loop that holds each object in an array slot to do the work on...
// here's the work on each item from the array, sets a gazillion settings on the prefabs
// names the prefab uniquely
// PLUS use Small_Individual_Setting_to_affect_object () on all of the prefabs as they're created
}
function Create_Row_Two_of_Prefab_of_type_1 () {
// create another dozen items, 1 at a time in a for loop
// here's the work on each item from the array, sets a gazillion settings on the prefabs
// names the prefab uniquely
}
function Create_Row_One_of_Prefab_of_type_2 () {
// create another dozen items, 1 at a time in a for loop
// here's the work on each item from the array, sets a gazillion settings on the prefabs
// names the prefab uniquely
// PLUS use Small_Individual_Setting_to_affect_object () on all of the prefabs as they're created
}
function Create__Row_Two_of_Prefab_of_type_2 () {
// create another dozen items, 1 at a time in a for loop
// here's the work on each item from the array, sets a gazillion settings on the prefabs
// names the prefab uniquely
}
function Create_Row_One_of_Prefab_of_type_1 () {
// Pass the newly created prefab to a new function so that it can be updated with stuff
Small_Individual_Setting_to_affect_object(createdPrefab);
}
function Small_Individual_Setting_to_affect_object(myPrefab : GameObject) {
//Update myPrefab...
}
I think that’s right, but there’s confusing running around my muddled mind. Two pieces of confusion:
In your first piece of code you’ve got a variable (createdPrefab);
Can this be a variable/array item like this (createdPrefab*) since this is how I’m cycling through the for loop updating/changing/creating settings for the prefab.* 2. In your second piece of code you’ve got a different variable (myPrefab); How does the function Small_Individual_Setting_to_affect_object know that it’s going to be working on the right prefab from the array? And how does the code in this small function look so that it’s affecting the prefab from the array?
The second function isn’t expecting a variable called myPrefab, it’s expecting a gameObject. Just like for a car to move it doesn’t need a V8 4.0 Litre GM it just needs a “motor” any will do.
So you can send any variable name to the second function so long as it’s a “gameObject”.
anythingcangohere : [B]GameObject[/B];
You can either send each item to the second function as createdPrefab (which is a gameobject) or you can send the entire array createdPrefab (which is an array of gameObjects) but you need to change the second function so it knows to expect gameObjects instead of a gameObject. ```
*function Small_Individual_Setting_to_affect_object(myPrefab : GameObject[]) {
Ok, I kind of understand your point with regards to point 1. That it’s expecting a game object… currently the game object, at the time I want the second function to play with it, is housed in an array, at position at the time I want to talk to it. But my question’s still the same… how to pass that game object housed in createdPrefab to the second function… Is this right: Ok? Cause it looks odd to me, having the in there… *_</em> <em><em>*Small_Individual_Setting_to_affect_object (createdPrefab[i]);*</em></em> <em>_* And then, secondly, how, when constructing the function Small_Individual_Setting_to_affect_object() do I ensure that it’s got control of, has a handle on, the object that I’m wanting it to change/add things to… for example is this stuff right… *_</em> <em><em>*function Small_Individual_Setting_to_affect_object(){ AddComponent(Rigidbody); rigidbody.useGravity = false; }*</em></em> <em>_* If this is right, how does the function know to add the component and then change it to useGravity = false to the thing that it’s holding? How do I know the function has got a hold of the object that I’ve sent it? btw I’ve read that Unity reference page on components about a dozen times, but it doesn’t go into the syntax or detail enough of how to use it for me to figure it out… GetComponent, addcomponent etc are a complete mystery to me because there’s simply no instance of it all being laid out where one can see the right order/syntax for doing everything that’s possible with this, clearly, very important part of Unity.
To define which gameObject you are affecting, put the array reference before the function. Since the array “createdPrefab[ ]” is a gameObject type, putting a number in it will reference the gameObject in the array list. So say “i” is 6, and you put “createdPrefab*”, unity will see it as the 7th game object on the array list of “createdPrefab[ ]” (remember, arrays always start with zero and not one).* createdPrefab*.AddComponent(Rigidbody);* createdPrefab*.rigidbody.useGravity = false;* Here is the script reference to AddComponent here If you’re not already familiar with the script reference, this should help you out a lot. You should be able to find the syntax of anything you need in the runtime classes here
THANKYOU ODD GAMES!!! You trooper! The coloring and capitilisation in your post helped me out in ways you cannot possibly imagine. Merry Christmas! I’ll be back referring to this until it’s buried into my neanderthal skull. thank you!!!
one more thing… (sigh) is it ok for me to put an array variable in there… so it’s this?
function
Small_Individual_Setting_to_affect_object([COLOR="lime"][COLOR="red"]gameObject[i][/COLOR][/COLOR] : [COLOR="#2e8b57"]GameObject[/COLOR]){
AddComponent(Rigidbody);
rigidbody.useGravity = false;
}
}
and then would this work to apply the function to the object in the array?
“but wait, there’s more …” (remember that guy from infomercials in oz about 10 or 15 years ago that became a celebrity just because of how annoying he was?)
the above assumes I have created and defined the contents of the array and the function in the same script on the same object that I’m then calling the function from.
Is there a way to do the same, but accessing the contents of an array created, defined and populated in a different script on a different object using a function that was also created/defined and sitting on a different object?
from your description of how things are done, shouldn’t it be possible for me to remove the “GetComponent” from the above and still achieve the same result? …
And if that’s the case, then how do I communicate with a particular configurable joint if I have two attached to the same object?
For example, a waist on a human joins two hips, both configurable joints. How would I edit specifically the settings of the left hip configurable joint?
You can’t quite do it that way, dissidently. Only some components are automatically exposed that way purely for ease-of-use. This page here lists them: Unity - Scripting API: GameObject
rigidbody, renderer, collider are some of these. I don’t believe there is one for ConfigurableJoint. Anything you can do directly to a GameObject is listed on that page, everything else you have to use GetComponent to retrieve.
Hey FizixMan thankyou! Do you happen to know how I can be selective with regards which configurable joint I’m talking to if I have two attached to the one object?