- Home /
Fixing a single call of an action (across 2 scripts)
I'm fairly new to coding and have been testing how strong I am by trying to build a 2D platformer. My code might be messy but instead of cleaning it up or anything I'd rather you would fix the bit I got wrong. So I have two scripts, my first is the character control: #pragma strict
var Respawn : Rigidbody2D;
var JumpHeight : float;
var Up : KeyCode;
var Down : KeyCode;
var Left : KeyCode;
var Right : KeyCode;
static var Grounded : int = 1;
function Update () {
if (Input.GetKey(Up) && Grounded == 1){
rigidbody2D.AddForce (new Vector2 (0, JumpHeight));
Grounded = 0;
}
else if (Input.GetKey(Left)){
rigidbody2D.velocity.x = -10;
}
else if (Input.GetKey(Right)){
rigidbody2D.velocity.x = 10;
}
else{
rigidbody2D.velocity.x = 0;
}
}
function OnCollisionEnter2D (GroundCol : Collision2D){
if (Grounded==0 && GroundCol.collider.tag == "Ground"){
Grounded = 1;
}
else if (Grounded==0 && GroundCol.collider.tag == "Wall"){
Grounded = 0;
}
else if (GroundCol.collider.tag == "OutOfBounds"){
transform.position.x = Respawn.position.x;
transform.position.y = Respawn.position.y;
var CamRestart = "reset";
FollowCamera.RestartCam (CamRestart);
}
}
And my second one is a camera control script: var cameraTarget : GameObject; var player : GameObject;
var CameraReset : Rigidbody2D;
var CameraR : Rigidbody2D;
var smoothTime : float = 1;
var cameraFollowX : boolean = true;
var cameraFollowY : boolean = true;
var cameraFollowHeight : boolean = false;
var cameraHeight : float = 2.5;
var velocity : Vector2;
private var thisTransform : Transform;
static var CamR : int = 0;
function Start ()
{
thisTransform = transform;
}
function Update (){
if (cameraFollowX){
thisTransform.position.x = Mathf.SmoothDamp (thisTransform.position.x, cameraTarget.transform.position.x, velocity.x, smoothTime);
}
if (cameraFollowY){
thisTransform.position.y = Mathf.SmoothDamp (thisTransform.position.y, cameraTarget.transform.position.y, velocity.y, smoothTime);
}
if (!cameraFollowX && cameraFollowHeight){
camera.transform.position.y = cameraHeight;
}
}
function RestartCam(CamRestart : String){
if (CamRestart == "reset"){
CameraReset.position.x = CameraReset.position.x;
CameraReset.position.y = CameraReset.position.y;
}
}
My problem is as I try to call the "RestartCam" function found just about unity shows me this message: "Assets/CharacterControll.js(38,30): BCE0020: An instance of type 'FollowCamera' is required to access non static member 'RestartCam'." Any ideas?
Answer by duck · Aug 05, 2014 at 04:46 PM
You're calling the function using the script's name itself "FollowCamera", rather than on a variable containing a reference to an instance of your script (which would be on an object in your scene).
Calling it on the script itself means you're trying to call it as a "static" method.
Instance, Reference, & Static are all terms that you need to become familiar with in order to understand what's going on.
It may also help you to understand these concepts more clearly if you follow Unity's convention of using an initial lower case letter for variable names, and an initial upper case letter for type names and function names.
Eg, your script is called "FollowCamera" but you need a variable that contains a reference to an instace of FollowCamera used in your scene, so the variable name could be "followCamera". This is a common thing to see in code, although it may look weird to you at first:
var followCamera : FollowCamera;
"followCamera" is the variable name, and "FollowCamera" is the variable's type
You could then drag the gameobject which has the FollowCamera script attached into that variable slot in the inspector. Your "followCamera" variable will now have a reference to the FollowCamera script on the object that you dragged in.
You'd then be able to call functions in the FollowCamera script by using the variable, like this:
followCamera.RestartCam();
How many times we had to $$anonymous$$ch the basics of OOP here ;)
Haha, thanks for the quick reply, its helped me a lot. Like I said I'm fairly new and I couldn't find any other help on the internet so I just resorted here. Thanks though.
Your answer
Follow this Question
Related Questions
Help with Functions 3 Answers
turn string into function with arguments? 1 Answer
Checking if a function has been called from another script 2 Answers
Using draggable actions in Editor 0 Answers
javascript equivalent of Action? 0 Answers