- Home /
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.
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.
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.
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
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.
instanciate your rigidbody, then add your script component with AddComponent (http://docs.unity3d.com/Documentation/ScriptReference/GameObject.AddComponent.html )
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
Follow this Question
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