• 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 Hordak · May 27, 2011 at 10:22 PM · staticinstancenonstatic

Create an Instance?

How do I create an instance? I'm struggling with t$$anonymous$$s script and now I get t$$anonymous$$s error.

An instance of type 'PlatformerController' is required to access nonstatic member 'canControl'

Here is the script:

 private var cameraScrolling : CameraScrolling;
 private var selected = 0;
 static var player1 : PlatformerController;
 static var player2 : PlatformerController;
 var targets : Transform[];
 
 function Awake () {
     cameraScrolling = GetComponent (CameraScrolling); 
     player1.GetComponent(PlatformerController);
     player2.GetComponent(PlatformerController);
     cameraScrolling.SetTarget (targets[0], true);
 }
 
 function Update () {
     if (Input.GetKeyUp(KeyCode.Alpha2))
         PlatformerController.canControl (player2, true); 
     PlatformerController.canControl (player1, false); 
     cameraScrolling.SetTarget (targets[1], true);
 }
 
 
 function LateUpdate () {
     if (Input.GetKeyUp(KeyCode.Alpha1))
         PlatformerController.canControl (player1, true);
     PlatformerController.canControl (player2, false); 
     cameraScrolling.SetTarget (targets[0], true);
 }

Please help

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · May 28, 2011 at 12:19 AM

Some notes on your script:

  • PlatformerController is a class so if you use t$$anonymous$$s name and put a dot be$$anonymous$$nd it you access the class, not a specific instance of that class.

  • You can access only static members if you use the classname.

  • In Awake you use your two variables (player1 & player2) to call GetComponent on the gameobject these scripts are attached, but you never set any values to these variables.

  • GetComponent returns the instance of the class that you specify inside the brackets if there is one on the given GameObject. You do not$$anonymous$$ng with the returned value so the whole call to GetComponent is useless.

  • Your if statements in Update/LateUpdate do only affect the next command because there are no curly-brackets, but it seems that you want both or maybe all three lines in the if block.

  • I don't see the reason why one "if" is in Update and the other in LateUpdate

  • We don't know how your PlatformerController script looks like but it seems the function "canControl" belongs to an instance(non static) and not to the class(static).

I'm not sure what canControl actually do but i guess it can enable the control for another player so your script should look like t$$anonymous$$s:

 private var cameraScrolling : CameraScrolling;
 private var selected = 0;
 static var player1 : PlatformerController;
 static var player2 : PlatformerController;
 var targets : Transform[];
 
 function Awake () {
     cameraScrolling = GetComponent (CameraScrolling); 
     cameraScrolling.SetTarget (targets[0], true);
 }
 
 function Update () {
     if (Input.GetKeyUp(KeyCode.Alpha2)) {
         player1.canControl (player2, true); 
         player2.canControl (player1, false); 
         cameraScrolling.SetTarget (targets[1], true);
     }
     if (Input.GetKeyUp(KeyCode.Alpha1)) {
         player1.canControl (player2, false); 
         player2.canControl (player1, true);
         cameraScrolling.SetTarget (targets[0], true);
     }
 }

I don't know where your player1 and player2 are initialized but they have to point to your PlatformerController-instances that are attached to your players. Is there any good reason why they are static? static members can't be assigned in the inspector.

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 Hordak · May 28, 2011 at 06:28 AM 0
Share

Thank you for a very detailed answer,the answer to all your why did you do this, is simply because I am a beginner and a noob :)

What I actually want is to enable or disable a var in 'PlatformerController' called 'canControl' throught this script. 'Platformer controller is a script placed on my players. canControl just enables the control for that player. The real problem is that I can choose between my two players, but the controls are enabled for both, so they both respond when I move them around...

avatar image aldonaletto · May 28, 2011 at 06:53 AM 1
Share

Is this PlatformerController a script of yours? I remember there's a canControl variable in the CharacterMotor script - can this be the one you want?

If you were to access the canControl variable in the CharacterMotor.js script, you should do something like this:

var script1: CharacterMotor = transform.GetComponent(CharacterMotor); script1.canControl = true;

If your script is really PlatformerController.js, just change the CharacterMotor to PlatformerController

avatar image
0

Answer by flaviusxvii · May 27, 2011 at 10:24 PM

canControl isn't a static method. You need an actual instance of a PlatformController to use that function.

Comment
Add comment · Show 1 · 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 Hordak · May 28, 2011 at 06:30 AM 0
Share

Yes I figured it was something like that, I am just perplexed on how to do this. How do I instance PlatformerController?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Unserialized private variable values from static instance persist when exiting play mode 1 Answer

How do i reference a non static singleton? 2 Answers

How do you get the results from an enum in a different script 1 Answer

How do i reference a non static singleton? 1 Answer

Having trouble with static references. 1 Answer


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