I have a code in C# for rotation(I have no idea how to use C#, i use java) that i got from somewhere(the only script in my game that isn’t written by me). And i need to use this line but it says there’s a problem. Can anyone tell me what it is??
Line:
if(gameObject.GetComponent("playerControl").controllable){
Error:
And to be honest, I have no idea what that error says because i never us C#, always java. But anyway, can someone please reply.
Edit:
The same problem is still there. This is how I put the variable for the GameObject in the C# file:
Code:
public GameObject thePlayer;
and my new line is:
Code:
if(thePlayer.GetComponent("playerControl").controllable){
And controllable is a boolean btw.
Ohhh, i cant access a variable in another script format can I??
if(GameObjectVar.GetComponent(“ScriptName”).VarName){
GameInit.GetComponent(TerrainGenerator).TerrainPieces*.Terrain;*
is an example of a line I used.
So GameObjectVar is a var I make up for the gameObject. And VarName is whatever my var from the other script is. Correct?
Edit: the same problem is still there. This is how I put the variable for the GameObject in the C# file:
public GameObject thePlayer;
and my new line is:
if(thePlayer.GetComponent("playerControl").controllable){
And controllable is a boolean btw.
Since C# is fully typesafe, when you call just GetComponent(“ScriptName”), it returns a “UnityEngine.Component” base object. You would need to explicitly cast it as your custom script name:
PlayerControl targetScript = (PlayerControl)gameObject.GetComponent("PlayerControl");
if (targetScript.controllable)
{
}
Even better is to use generics:
PlayerControl targetScript = gameObject.GetComponent<PlayerControl>();
if (targetScript.controllable)
{
}
New problem. Now the error is:
error CS0246: The type or namespace name `playerControl' could not be found. Are you missing a using directive or an assembly reference?
I think that’s saying it cant find it. But i am 150% sure that the JavaScript “playerControl” is ther and I changed the line to:
playerControl targetScript = gameObject.GetComponent<playerControl>();
if (targetScript.controllable)
{
Your JavaScript might have to be put in a “plugins” folder. Essentially, C# doesn’t see it unless it’s compiled first.
EDIT: generally speaking, it’s better if you can stick to having all your code in a single language.
Ok well, can anyone tell me where i can get the MouseLook script in JS? because I have no idea how to translate it from C# to JS.