Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Claymstamper · May 03, 2016 at 07:48 PM · scripting problemsyntaxparser

How can I get this code to recognize coll in my update function?

using UnityEngine; using System.Collections;

public class Ninja : MonoBehaviour {

 float speed = 7.0f;

 // Use this for initialization
 void Start () {
     transform.position = new Vector3 (2, 8, -5);

 
 }

 // Update is called once per frame
 void update () {
  
     if (coll.collider == false & Input.GetKey (KeyCode.UpArrow)) {
         Debug.Log ("up");

     }    

 }
 void faceRight () {
     transform.localRotation = Quaternion.Euler (0, 0, 0);
 }
 void OnCollisionStay2D(Collision2D coll){
     // If the Collider2D component is enabled on the object we collided with.
     if (coll.collider == true) {
         if (Input.GetKey (KeyCode.LeftArrow)) {
             Debug.Log ("left arrow");
             transform.position += Vector3.left * speed * Time.deltaTime;
         }
         if (Input.GetKey (KeyCode.RightArrow)) {
             Debug.Log ("right arrow");
             transform.position += Vector3.right * speed * Time.deltaTime;
         }

     } 
 }

}

Comment
Add comment · Show 3
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 FortisVenaliter · May 03, 2016 at 08:14 PM 0
Share

Can you describe the problem? It's pretty hard to diagnose and help when we don't know the problem.

avatar image Cherno · May 03, 2016 at 08:43 PM 0
Share
  1. Why is the Update function written with a lower case u? ("update")?

  2. Why are you using a variable "coll" in update when it is only defined in OnCollisionStay2D?

avatar image Claymstamper · May 04, 2016 at 03:05 AM 0
Share

Sorry for any errors or sloppiness. I'm fairly new and trying to learn. But yeah, so my goal here is to apply new physics to my sprite only when the following conditions are met; $$anonymous$$y sprite is not touching anything and up arrow is held. I just want to figure out how to get my update function to tell that it's not touching anything.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by FortisVenaliter · May 04, 2016 at 05:56 PM

Okay, so the problem here is called Scope. There are a bunch of articles you can look up online if you use that keyword, but here's a basic breakdown:

Variables ONLY exist within the block that they are defined. In your case, the coll variable is defined in the OnCollisionStay2D function as a parameter, so it can't be accessed in any other function. If you need to use it in the update function, you need to cache it off as a class-scope variable (like your speed float), which will allow it to be accessed from any function in that class. But keep in mind class variables are persistent with the instance, so if you set it in the OnCollisionStay2D function, it will stay set, and will not revert to null when there isn't a collision, so you'll have to handle un-setting it too.

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Encyclopedia System 1 Answer

Monodevelop Problem 5.5.2 0 Answers

How do I change a sprite opacity over time? 0 Answers

You know the drill: my shoot script isn't working (again.) 2 Answers

Any way to set custom curves on an AudioSource in Webgl ? 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