x


Passing a Variable to an Instance

Hello,

I know there have been a lot of posts on this subject, and I have read at least half a dozen, and I just don't get it. The whole GetComponent deal is lost on me. I have a feeling because most of the posts reference something like GetComponent(ScriptHere) and I don't know which script to put there. Plus the examples I have seen haven't discussed if the code should go in the source or destination file. I am a NOOB to Unityscript, but I've coded in many other languages before, and I can't wrap my head around this one bit.

Here is a the source file where I have made a button to Instantiate a prefab called MakeSpellObject.js.

var prefab:GameObject;
var spellName;

function OnGUI () {

    if (GUI.Button (Rect (20,40,90,20), "Create Spell")) {
       var pos = Vector3(13,2,0); 
       spellName = Instantiate(prefab, pos, Quaternion.identity);
       var spell = "spellPlay";
       Debug.Log("variable is set to " + spell);
    }

}

The destination file is called DragSpell.js, and here is an excerpt of the relevant code.

function OnMouseDown () { canMove = true; myTransform.Translate(Vector3.up*addHeightWhenClicked); gravitySetting = myRigidbody.useGravity; freezeRotationSetting = myRigidbody.freezeRotation; myRigidbody.useGravity = false; myRigidbody.freezeRotation = freezeRotationOnDrag; yPos = myTransform.position.y;

GetComponent("MakeSpellObject").spell = GetComponent("MakeSpellObject").spell;

Debug.Log(spell);

}

If someone would take a look at my code that would be incredible.

Thanks, Steve

more ▼

asked Jun 08 '11 at 04:56 AM

Grasshorse gravatar image

Grasshorse
16 9 10 11

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

wasn't quite sure what you meant exactly by 'passing parameters' to instantiate. i assume you mean parameters for setting up the object, aside from what you'd pass into Instantiate, i.e. position, rotation, etc. So here's a few different things I might do on a given usage of Instantiate in my code:

var obj=GameObject.Instantiate(prefab) as GameObject;
obj.transform.parent=transform; //makes the new obj a child of this one
obj.renderer.enabled=false; //disables the object initially until enabled later, i.e. if pre-caching objects

var fooCom:Foo=obj.GetComponent(Foo);
fooCom.fooMethod();
fooCom.fooVar="whatevs";

,Instantiate will return the newly created GameObject for you. In strict typing, you'll have to add "as GameObject" to explicitly cast it because Instantiate returns just the bottom-level Object interface.

Once you have the GameObject reference, you can call GetComponent on that, passing in the name of the script file(don't use quotes if you can help it, it's faster) that is this other component you are trying to get. If the prefab you instantiated has the script attached, you'll get a reference to that script, and you can then call methods and access vars from it like it's a C++ class.

more ▼

answered Jun 08 '11 at 06:37 AM

TomHunt gravatar image

TomHunt
138 4 4 10

(comments are locked)
10|3000 characters needed characters left

OK, So I got the code to work based off of what Tom posted, problem is, the variable was brought into the code that instantiated the instance of the prefab, but not into the instance itself. Here is the revised code for MakeSpellObject.js script.

function OnGUI () {

if (GUI.Button (Rect (20,40,90,20), "Create Hex")) {
    var pos = Vector3(13,2,0);
    var spellName = GameObject.Instantiate(prefab, pos, Quaternion.identity) as GameObject;
    spell = spellName.GetComponent("DragSpell").spellArray[0];


}
}

And then I put this array code in the DragSpell.js script file.

var spellArray = new Array ("spellBubble", "spellLoop", "spellPlay", "spellStep", "spellStill");

So when I did Debug.Log(spellArray[0]) in the MakeSpellObject.js it came in beautifully so the GetComponent("DragOject"); code worked, however it didn't exist in the instance that was created. So I have had some success, but the end result is still not accomplished of passing a dynamic value of the spellArry into an instanced object. Anyone have any ideas if that is possible? I'm starting to think not.

Best, Steve

more ▼

answered Jun 09 '11 at 03:06 PM

Grasshorse gravatar image

Grasshorse
16 9 10 11

When you instantiate a prefab, what you get is a clone of the prefab. It's up to the calling code to change the values of it from their defaults to the desired configuration. If you want an abstraction to pass values into, you could extract all the instantiation/configuration code out into a sort of constructor function, and call that from your GUI.Button if{} block.,,you haven't initialized the array in your example. it's going to give you the default values from the prefab if you try to read it straight out.

Instantiating a prefab is just going to make a clone of the prefab. it's going to have all the same values as the prefab. you write to those values in the cloned object after it is instantiated. it might be best to extract all this out to a separate function and use that as a sort of constructor for that particular configuration of instantiation.

Jun 09 '11 at 04:01 PM TomHunt

In the future, please respond to answers either by commenting on the answer or (until you gain that ability with over 10(?) rep) adding comments to, or editing, your question.

By adding comments as answers, it makes it harder for others to read the thread.

Jun 09 '11 at 04:04 PM Chris D

Oh, gotcha. I didn't even see that. Thanks for the tip Chris.

Jun 09 '11 at 04:15 PM Grasshorse

Thanks for your response Tom. That points me in the right direction. Thanks for your help on this!

Jun 09 '11 at 05:19 PM Grasshorse
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1683
x825
x406

asked: Jun 08 '11 at 04:56 AM

Seen: 1690 times

Last Updated: Jun 09 '11 at 05:19 PM