Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 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 think this is the case) or I am unable to translate it into C# *(I think this 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 = this.turnRate;
             tempWpnSpd = this.weaponSpeed;
             tempTTR = this.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 = this.turnRate; tempClone.tempWpnSpd = this.weaponSpeed; tempClone.tempTTR = this.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 = this.turnRate;
             scriptSB.weaponSpeed = this.weaponSpeed;
             scriptSB.timeToReload = this.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
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
0
Best Answer

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

I think 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 = this.turnRate;
             scriptSB.weaponSpeed = this.weaponSpeed;
             scriptSB.timeToReload = this.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
Add comment · 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
1
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 something like this tempTurn = tempClone.GetComponents<scriptStarbaseControls>(turnRate); in C#, you need to have for example: tempTurn = tempClone.GetComponents<theTypeOfTheComponent>().theActionOnThisType(); **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 this 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

This 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 think 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
Add comment · 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. $$anonymous$$odifying 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.

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

Null Reference Exception on instantiated objects script. 1 Answer

Endless 3D plane repetition animated by script is not moving 0 Answers

Limiting respawns on scene 1 Answer

Accessing instantiated GOs nested in prefabs with GetComponent 4 Answers

How to instantiate a prefab after a certain action happened? 2 Answers


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