• 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 Mars530x · Nov 26, 2022 at 04:27 PM · scripting problembugbugsscripterror

What is wrong with this code?

What's wrong with t$$anonymous$$s code? I used tutorial but it did not show any help with bugs.


 public Image healthBarImage;
 public PlayerController player;
 Tween tween;
 bool moving = false;
 public CanvasGroup parentCanvasGroup;
 void Update() {
   transform.rotation = startRotation;
   if ( moving && player.characterController.velocity.magnitude < 0.1f ) {
     if ( tween != null ) {
       tween.Kill();
     }
     moving = false;
     tween = parentCanvasGroup.DOFade( 1f, 0.75f );
   } else if ( !moving && player.characterController.velocity.magnitude > 0.15f ) {
     if ( tween != null ) {
       tween.Kill();
     }
     moving = true;
     tween = parentCanvasGroup.DOFade( 0f, 0.75f );
   }
 }
 


Comment
Add comment · Show 4
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 amal_nj · Nov 26, 2022 at 04:47 PM 0
Share

Please provide more details about the errir you're getting

avatar image logicandchaos · Nov 26, 2022 at 09:31 PM 0
Share

Always post the full error, the error usually tells you exactly what is wrong and where. Not the error code which is cs1030 or whatever.. what people need is the stull like "null reference exception at something.cs 10,20" the error message says exactly what the issue is, then says what script it's in then what line and even where on that line.

avatar image Mars530x logicandchaos · Nov 28, 2022 at 05:08 PM 0
Share

My error is: Could not find "Healthbar" Another: Could not find "Player"

avatar image logicandchaos Mars530x · Nov 29, 2022 at 09:11 PM 0
Share

that's still not the full error..

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by SurreyMuso · Nov 26, 2022 at 05:15 PM

First of all, for an experienced poster, t$$anonymous$$s is a horrible layout! I t$$anonymous$$nk you know better how to post code, given how active you are... Anyway, on to the code.

There's no class definition and, hence no Monobehaviour so transform is undefined
There's no Class called Image
startRotation is not defined
characterController appears to be a component but you haven't got a GetComponent to find it

Fix those then come back again and please use the Code Sample button :o)

Comment
Add comment · Show 9 · 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 Mars530x · Nov 26, 2022 at 05:23 PM 0
Share

Weird. This is from tutorial, but I do understand. It also looks like I'm in a different editor than the tutorial.

avatar image logicandchaos Mars530x · Nov 26, 2022 at 09:26 PM 1
Share

the editor won't matter for syntax errors, they will always happen for the same reasons.

avatar image Mars530x logicandchaos · Nov 29, 2022 at 03:46 PM 0
Share

THANKS. Well, I followed this one tutorial's code perfectly. Guess what? His screen (Differing editor) had no errors. Mine has 5 or 6 lol

Show more comments
avatar image
1

Answer by sacredgeometry · Nov 30, 2022 at 02:33 PM

There is a lot wrong with it

  1. Its a code fragment i.e. its not all the relevant code

  2. It doesnt run

  3. The formatting is horrific

  4. Its a bit of a mess (see below for specifics)


The issue with it running has already been answered so I will try to address the formatting issues.


  1. Method parameters should not be padded with w$$anonymous$$te space

  2. Scoping brackets i.e. { } should be placed on a new line in C#

  3. Public members should be in Pascal Case

  4. Private members should be in camel case (optionally preceded with an underscore)

  5. I suggest being explicit and consistent with member access modifiers

  6. I suggest being consistent with use of spacing/ new lines

  7. You dont need to repeat code i.e. the tween null check is written twice unnecessarily.

  8. I suggest not binding the logic for determining if somet$$anonymous$$ng is moving with the logic for what you want to happen as a result of that.

  9. T$$anonymous$$s Monobehaviour doesnt seem to be a well organised entity it seems to just be a hodgepodge of responsibilities.


Example


 using UnityEngine;
 using System.Collections;
 
 public class MySomet$$anonymous$$ngClass : MonoBehaviour
 {   
     public CanvasGroup ParentCanvasGroup;
     public Image HealthBarImage;
     public PlayerController Player;
     private Tween tween;
 
     // Note t$$anonymous$$s logic is a little odd in your code there is a dead zone ... I have removed it for the sake of argument.
     private bool isMoving => Player.characterController.velocity.magnitude > 0.15f;
 
     private void Update() 
     {
         transform.rotation = startRotation;
 
         if (tween == null) 
             return; // Or throw exception if t$$anonymous$$s is not supposed to happen ... or you know just set it in here so you are sure it cant be null.
         
         tween.Kill();
 
         if(isMoving)
         {
             tween = ParentCanvasGroup.DOFade(1f, 0.75f);
         }
         else 
         {
             tween = ParentCanvasGroup.DOFade(0f, 0.75f);
         }
     }
 }


Comment
Add comment · Show 3 · 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 Mars530x · Nov 30, 2022 at 03:55 PM 0
Share

Thank youuuuuu!

avatar image gjf · Dec 01, 2022 at 03:13 PM 0
Share

The null check will fail...

avatar image sacredgeometry gjf · Dec 01, 2022 at 05:32 PM 0
Share

Well caught, sorry writing code in this GUI is awful

avatar image
0

Answer by Artmarab · Nov 26, 2022 at 05:14 PM

T$$anonymous$$s line tween = parentCanvasGroup.DOFade( 0f, 0.75f ); looks fishy to me. In the documentation doesn't appear a DOFade method for CanvasGroup but also you're trying to assign its result to a variable of type tween and that doesn't look ok.

EDIT: also please try to use a correct formating when inserting code.

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 Mars530x · Nov 26, 2022 at 05:24 PM 0
Share

Ya, thanks.

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

280 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

This code is broken!!! 1 Answer

First script plays animation but second script stops if from playing said animation 1 Answer

Adding a ui text as a child of canvas disables all child buttons and makes them break 1 Answer

How do I inherit certain varibles and functions from another script. 1 Answer

Level Select screen Messed Up - Please help! 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