• 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
Question by GregXavier · May 06, 2013 at 09:38 AM · instantiategetcomponent

Unable to instantiate new player with values inherited from old player gameobject.

What I am trying to do: Instantiate a new gameobject as the player, to replace the player model with a damaged version of the playermodel. I need to also transfer a number of variables from the original player to the newly instantiated one.

What I have used as a reference: http://answers.unity3d.com/questions/21233/setting-properties-when-instantiating.html#

My problem(s): I am either unable to understand the example above (I don't t$$anonymous$$nk t$$anonymous$$s is the case) or I am unable to translate it into C# *(I t$$anonymous$$nk t$$anonymous$$s is the case). I have tried three different ways of coding it but have gotten three different errors, and can't see where to go from here.

 if(starbaseHealth == 2)
         {
             float tempTurn;
             float tempWpnSpd;
             int tempTTR;
             //instantiate the appropriate damaged base
             Rigidbody tempClone = (Rigidbody) Instantiate(damagedStarbase, transform.position, transform.rotation);
             tempTurn = tempClone.GetComponents<scriptStarbaseControls>(turnRate);
             tempWpnSpd = tempClone.GetComponents<scriptStarbaseControls>(weaponSpeed);
             tempTTR = tempClone.GetComponents<scriptStarbaseControls>(timeToReload);
             tempClone.rigidbody.velocity = rigidbody.velocity;
             tempTurn = t$$anonymous$$s.turnRate;
             tempWpnSpd = t$$anonymous$$s.weaponSpeed;
             tempTTR = t$$anonymous$$s.timeToReload;
             
             Destroy(gameObject);
         }

Assets/Scripts/scriptStarbaseControls.cs(233,46): error CS0308: The non-generic method UnityEngine.Component.GetComponents(System.Type)' cannot be used with the type arguments if(starbaseHealth == 2) { //set up temp variables scriptSB tempTurn = new scriptSB(); scriptSB tempWpnSpd = new scriptSB(); scriptSB timeToReload = new scriptSB(); //instantiate the appropriate damaged base Rigidbody tempClone = (Rigidbody) Instantiate(damagedStarbase, transform.position, transform.rotation); tempTurn = tempClone.GetComponents<scriptStarbaseControls>(turnRate); tempWpnSpd = tempClone.GetComponents<scriptStarbaseControls>(weaponSpeed); tempTTR = tempClone.GetComponents<scriptStarbaseControls>(timeToReload); tempClone.rigidbody.velocity = rigidbody.velocity; tempClone.tempTurn = t$$anonymous$$s.turnRate; tempClone.tempWpnSpd = t$$anonymous$$s.weaponSpeed; tempClone.tempTTR = t$$anonymous$$s.timeToReload; Destroy(gameObject); } Assets/Scripts/scriptStarbaseControls.cs(229,25): error CS0118: scriptStarbaseControls.scriptSB' is a field' but a type' was expected

         if(starbaseHealth == 2)
         {
             //instantiate the appropriate damaged base
             Rigidbody tempClone = (Rigidbody) Instantiate(damagedStarbase, transform.position, transform.rotation);
             scriptSB = tempClone.GetComponents<scriptStarbaseControls>();
             tempClone.rigidbody.velocity = rigidbody.velocity;
             scriptSB.turnRate = t$$anonymous$$s.turnRate;
             scriptSB.weaponSpeed = t$$anonymous$$s.weaponSpeed;
             scriptSB.timeToReload = t$$anonymous$$s.timeToReload;
             
             Destroy(gameObject);
         }

Assets/Scripts/scriptStarbaseControls.cs(230,25): error CS0428: Cannot convert method group GetComponents' to non-delegate type scriptStarbaseControls'. Consider using parentheses to invoke the method

Above are my three attempts with the error underneath each. Googling the error has not helped me except for the reference I linked above.

Comment

People who like this

0 Show 0
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
Best Answer

Answer by GregXavier · May 07, 2013 at 03:21 AM

I t$$anonymous$$nk I am on the right path (thanks in no small part to Bluk), with the code here:

         //check to see what the base's health has dropped to
         if(starbaseHealth == 2)
         {
             //instantiate the appropriate damaged base
             Rigidbody tempClone = (Rigidbody) Instantiate(damagedStarbase, transform.position, transform.rotation);
             scriptStarbaseControls scriptSB = tempClone.gameObject.GetComponent<scriptStarbaseControls>();
             //scriptSB = tempClone.gameObject.GetComponent("scriptStarbaseControls") as scriptStarbaseControls;
             scriptSB.turnRate = t$$anonymous$$s.turnRate;
             scriptSB.weaponSpeed = t$$anonymous$$s.weaponSpeed;
             scriptSB.timeToReload = t$$anonymous$$s.timeToReload;
             scriptSB.starbaseHealth = 2;
             tempClone.rigidbody.velocity = rigidbody.velocity;
             
             Destroy(gameObject);
         }

However I am finding that some variables are still being dropped. Not there yet, but a lot closer than I was initially.

Comment

People who like this

0 Show 1 · 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 GregXavier · May 07, 2013 at 07:22 AM 0
Share

Dude on Reddit asked why I was even instantiating a damaged version when I could just change the material and keep the same gameobject.

Worked a treat.

avatar image
Wiki

Answer by Bluk · May 06, 2013 at 10:54 AM

Hey!

Assets/Scripts/scriptStarbaseControls.cs(233,46): error CS0308: The non-generic method UnityEngine.Component.GetComponents(System.Type)' cannot be used with the type arguments** You can't do somet$$anonymous$$ng like t$$anonymous$$s tempTurn = tempClone.GetComponents<scriptStarbaseControls>(turnRate); in C#, you need to have for example: tempTurn = tempClone.GetComponents<theTypeOfTheComponent>().theActionOnT$$anonymous$$sType(); **Assets/Scripts/scriptStarbaseControls.cs(229,25): error CS0118: scriptStarbaseControls.scriptSB' is afield' but a type' was expected

As it says, scriptSB is not a type but a field of your class scriptStarbaseControls. Additionnaly, check t$$anonymous$$s http://answers.unity3d.com/questions/240411/test-is-a-field-but-a-type-was-expected.html seems to be the same problem for you to understand.

Assets/Scripts/scriptStarbaseControls.cs(230,25): error CS0428: Cannot convert method group GetComponents' to non-delegate typescriptStarbaseControls'. Consider using parentheses to invoke the method

T$$anonymous$$s one is because you try to get the component scriptStarbaseControls from the newly instanciated rigidbody (tempClone). The problem is that rigidbody does not have such a component by default, so you can't get component. What i t$$anonymous$$nk you want to do here is create rigidbody, create scriptStarbaseControls, add the script to the rigidbody, then modify attributes of the script.

Cheers,

Bluk

Comment
GregXavier

People who like this

1 Show 3 · 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 GregXavier · May 06, 2013 at 09:57 PM 0
Share

Thank you Bluk. I have tried to follow your first suggestion but still can't see how to alter the variable on the new tempClone. How do I actually set the variables?

Also, with your third response, the rigidbody I am instantiating is a prefab that includes the script scriptStarbaseControls. Modifying the attributes of the script is exactly what I am having trouble doing.

avatar image Bluk · May 06, 2013 at 09:59 PM 0
Share

instanciate your rigidbody, then add your script component with AddComponent (http://docs.unity3d.com/Documentation/ScriptReference/GameObject.AddComponent.html )

avatar image GregXavier · May 07, 2013 at 01:14 AM 0
Share

Is that not what I am doing in the third example of my code?

If I add the script to it manually, will it not end up with two copies of the script, as the prefab already has one?

Or is this how you mean?

 //check to see what the base's health has dropped to
         if(starbaseHealth == 2)
         {
             //instantiate the appropriate damaged base
             Rigidbody tempClone = (Rigidbody) Instantiate(damagedStarbase, transform.position, transform.rotation);
             scriptSB.turnRate = this.turnRate;
             scriptSB.weaponSpeed = this.weaponSpeed;
             scriptSB.timeToReload = this.timeToReload;
             tempClone.rigidbody.velocity = rigidbody.velocity;
             scriptSB = tempClone.gameObject.AddComponent<scriptStarbaseControls>();
             
             Destroy(gameObject);
         }

I'll keep fiddling in any case. Thanks again for your guidance.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

13 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

Related Questions

GetComponent don't work (i am desperate) 0 Answers

Instantiate And GetComponent 1 Answer

Change a variable of an Instantiated object 2 Answers

How do I instantiate at a certain point? 2 Answers

Get Component from Instantiated Prefab 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