Error CS1525: Unexpected Symbol ')' on lines 13 and 14
I am pretty sure there is a simple fix that I cannont see. Here is my code:
using UnityEngine;
using System.Collections;
public class FlyShip : MonoBehaviour {
ShipMove MoveScript;
MouseLook AimScript;
void Update (){
if (Input.GetKeyDown("f")){
print ("Entering Ship");
MoveScript = GameObject.Find("PlayerShip").GetComponent<"ShipMove">();
AimScript = GameObject.Find("PlayerShip").GetComponent<"MouseLook">();
MoveScript.enabled = !MoveScript.enabled;
AimScript.enabled = !AimScript.enabled;
}
}
}
Answer by Dave-Carlile · Jun 17, 2016 at 11:47 AM
GetComponent<"ShipMove">
The part inside is a class name, not a string. So you want:
GetComponent<ShipMove>();
And the same sort of change on the next line.
Also GameObject.Find
can have some poor performance. They way you're using it is probably fine since you only execute it when F is pressed, but since you're calling it twice you can improve things a bit by getting a reference first and then using that reference to find the components.
// use Find once to get a reference to the ship object into the ship variable
GameObject ship = GameObject.Find("PlayerShip");
// use the ship variable to get the components
MoveScript = ship.GetComponent<ShipMove>();
AimScript = ship.GetComponent<MouseLook();
....
And so on
Thanks for the answer AND the extra performance advice!
Answer by mikelortega · Jun 17, 2016 at 11:52 AM
Remove the quotation marks to the GetComponent type, the text between .
MoveScript = GameObject.Find("PlayerShip").GetComponent<ShipMove>();
AimScript = GameObject.Find("PlayerShip").GetComponent<MouseLook>();
Your answer
Follow this Question
Related Questions
Struggling with any input in UWP 0 Answers
Missing Reference Exception... please help, I am new to this! 2 Answers
CS1023 An embedded statement may not be a declaration or labeled statement help 1 Answer
Error stage @username 1 Answer
InvalidCastException: Cannot cast from source type to destination type. 1 Answer