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 Rick74 · Oct 04, 2013 at 02:35 PM · javascripterror

How am I getting this error??

The error is;

" Operator '+' cannot be used with a left hand side of type 'float' and a right hand side of type 'Object'."

the code in question is;

 #pragma strict
 
 var coolTimeMax:            float         = 8.0f;
 var isUseSkill:                boolean     = false;
 var buttonClickTrue:        AudioClip;
 var buttonClickFalse:        AudioClip;
 
 private var timer:            float       = 1.0f;
 private var coolTime:        float         = 0.0f;
 private var silder:            UISlider     = null;
 private var getCam:            GameObject;
 
 private var soundRate:         float         = 0.0;                    // variable to hold sound rate
 private var soundDelay:     float         = 0.0;
 
 function Start () 
 {
     silder = GetComponent(UISlider);
     getCam = GameObject.FindGameObjectWithTag("MainCamera");
 }
 
 function Update ()
 {
     silder.sliderValue = timer;
     
     if (UICamera.hoveredObject != null && UICamera.hoveredObject.name == ("btn_Farmer") && Input.GetMouseButton(0) && !isUseSkill && timer == 1)
     {
         PlaySound ( buttonClickTrue, 0 );
         getCam.GetComponent(InGameGUI).SetBuildChoice ( gameObject );
     }
     if (isUseSkill) 
     {
         StartTimer ();
         isUseSkill = false;
     }
 }
 
 function StartTimer ()
 {
     var t: float = 0.0f;
         
     while (t < 1)
     {
         t += Time.deltaTime / coolTimeMax;
         timer = Mathf.Lerp (0, 1, t);
         yield;
     }
 }
 
 function PlaySound ( soundName, soundDelay )                        // calls to play sound based on sound file name attached to object
 {
     if ( !audio.isPlaying && Time.time > soundRate )                // sets sound rate
     {
         soundRate = Time.time + soundDelay;                            // option to add a delay to the sound before it starts playing
         audio.clip = soundName;
         audio.Play ();
         yield WaitForSeconds ( audio.clip.length );
     }    
 }

the exact line this error is pointing too is;

 soundRate = Time.time + soundDelay;

This makes no sense! last I soundRate is a float, Time.time is a float, and soundDelay is a float! Where does it get this "Object" type from?!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Eric5h5 · Oct 04, 2013 at 02:43 PM

It makes complete sense since you haven't defined the types for the variables in your PlaySound function. You have two soundDelay variables; a global one and a local one in the PlaySound function.

Comment
Add comment · Show 5 · 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 ArkaneX · Oct 04, 2013 at 02:48 PM 1
Share

Was about to post an answer, but then saw Eric already posted.

You need to change

 function PlaySound ( soundName, soundDelay )

to

 function PlaySound ( soundName, soundDelay : float )

soundDelay in the function declaration is not the same as soundDelay declared in a script, so compiler doesn't know what's its type. You have to specify it.

avatar image Rick74 · Oct 04, 2013 at 02:49 PM 0
Share

I'm afraid I don't follow you Eric. I've used this function several times and have never gotten an error like this.

avatar image Eric5h5 · Oct 04, 2013 at 02:51 PM 0
Share

@ArkaneX: almost...you need to define the types of all variables.

 function PlaySound ( soundName : AudioClip, soundDelay : float )
avatar image Rick74 · Oct 04, 2013 at 02:51 PM 0
Share

Ok thanks for the clear up guys, This is weird because I've used this function as is several times before and never have I had to declare the type inside the Function command. But what you said is true, Ark, once I typed in float, the error went away.

Thanks!

avatar image ArkaneX · Oct 04, 2013 at 03:04 PM 0
Share

@Eric5h5 - it's not necessary, because without specifying AudioClip for soundName it works properly, although with implicit downcast warning.

But generally I agree with you - soundName (or any other parameters) should be declared explicitly to prevent warnings.

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

18 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

Related Questions

Issue with OnTriggerEnter. 1 Answer

expecting (, found 'Update' and unexpected char: 0x200B. 2 Answers

Player not taking any damage 2 Answers

Texture2d[]: Array index is out of rang (Javascript) 3 Answers

Making a pick up item that is timed, that moves,scales and rotates 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