Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 MMRREE · Aug 05, 2014 at 04:20 PM · functionbasicactiontwo

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?

Comment
Add comment · Show 1
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 robertbu · Aug 05, 2014 at 04:26 PM 0
Share

http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

http://unitygems.com/script-interaction-tutorial-getcomponent-unityscript/

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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();


Comment
Add comment · Show 2 · 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 Bunny83 · Aug 05, 2014 at 04:53 PM 0
Share

How many times we had to $$anonymous$$ch the basics of OOP here ;)

avatar image MMRREE · Aug 06, 2014 at 06:55 AM 0
Share

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

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

3 People are following this question.

avatar image avatar image avatar image

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


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