Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Grasshorse · Jun 08, 2011 at 04:56 AM · instantiatevariablegetcomponent

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

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by TomHunt · Jun 08, 2011 at 06:37 AM

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.

Comment
Add comment · 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
0

Answer by Grasshorse · Jun 09, 2011 at 03:06 PM

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

Comment
Add comment · Show 4 · 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 TomHunt · Jun 09, 2011 at 04:01 PM 1
Share

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.

avatar image Chris D · Jun 09, 2011 at 04:04 PM 1
Share

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.

avatar image Grasshorse · Jun 09, 2011 at 04:15 PM 0
Share

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

avatar image Grasshorse · Jun 09, 2011 at 05:19 PM 0
Share

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Only change a variable on the instaniated object not the prefab. 0 Answers

Want to add ship's velocity to Instantiated projectile 2 Answers

How to call a function on an instance 1 Answer

Using a top level variable in a class 0 Answers

Is it possible to use a variable as a Type? 1 Answer


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