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 cre8or · May 09, 2016 at 09:03 PM · c#scripting problemerrormethodsarguments

Help with an error in scripting - extreme newbie

I have an error that I am unable to figure out: rror CS0308: The non-generic method `UnityEngine.GameObject.GetComponent(System.Type)' cannot be used with the type arguments

Here is the code giving me grief:

     public void Q1False ()
     {
         Q1FalseButton.SetActive (false);
         Debug.Log ("False button clicked");
         Q1B = true;
         Debug.Log ("Q1B set to true");
         Q1TrueButton.GetComponent<BlinkEffect> (true);
         Debug.Log ("True set to blink");
 
     }

I've also tried replacing GetComponent with AddComponent with the same results.

BlinkEffect is essentially a .cs script attached to the UI and linked to the OnClick event of the False button. When the false button is clicked, I want the true button to start blinking, indicating the answer was actually True.

I know my code is probably laborious, but I am very new to scripting, coming from a design background, and I am trying to learn. I am reading the book: Learning C# Programming with Unity 3D, but the process is long, and I'm only on Chapter 3 - I've learned about ints, floats and doubles and the numbers they can handle and when to use them. But that doesn't help me with this problem.

I'm hoping someone with more knowledge than me could help me out and point me in the right direction.

I did find this article: http://blogs.unity3d.com/2015/01/21/addcomponentstring-api-removal-in-unity-5-0/ but I don't completely understand how to use this information to fix my issue.

Please help. Respectfully, Kimber

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

3 Replies

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

Answer by TBruce · May 10, 2016 at 03:30 AM

@cre8or

Here is a quick test script that I threw together using BlinkEffect of the UI Addons package. I created a UI Button component along with a UI Text component and BlinkEffect component.

This script will take the BlinkEffect either as a property through the inspector or if the script is attached to the same GameObject then it will get a reference to the the BlinkEffect component using GetComponent(). The ToggleBlink(() function toggles the blinking when the button is pressed.

 using UnityEngine;
 using System.Collections;
 using UIAddons;
 
 public class Test : MonoBehaviour
 {
     public BlinkEffect blinkEffect;
 
     // Use this for initialization
     void Start ()
     {
         if (blinkEffect == null)
         {
             blinkEffect = gameObject.GetComponent<BlinkEffect>();
         }
     }
     
     public void ToggleBlink ()
     {
         if (blinkEffect != null)
         {
             blinkEffect.isBlinking ^= true;
         }
     }
 }



Comment
Add comment · Show 16 · 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 cre8or · May 10, 2016 at 04:51 AM 0
Share

@$$anonymous$$avina I apologize for my ignorance, but I am just learning: I understand some operators, for example: == is equivalent to != is not equal

=

is greater than or equal to

|| or && and % modal ... Can be used to describe time But I have never seen the "little hat" used before. What does ^= mean?
avatar image TBruce cre8or · May 10, 2016 at 05:07 AM 0
Share

^ is the xor (exclusive or) operator. Bitwise operators.

So

 false ^ true = true
 true ^ true = false
 0 ^ 1 = 1
 1 ^ 1 = 0

This

 blinkEffect.isBlinking ^= true;

is just a quick and efficient way of toggling a variables value using the bitwise exclusive or mathematical operator.

avatar image cre8or TBruce · May 11, 2016 at 05:39 AM 0
Share

Thank you very kindly @$$anonymous$$avina for that link.

avatar image tanoshimi cre8or · May 10, 2016 at 06:21 AM 1
Share
 blinkEffect.isBlinking = !blinkEffect.isBlinking;

would achieve the same result.

avatar image TBruce tanoshimi · May 10, 2016 at 08:39 AM 0
Share

$$anonymous$$ore than one way to skin a cat,

Show more comments
avatar image TBruce cre8or · May 11, 2016 at 06:35 PM 0
Share

It looks like we are getting a bit off topic here. @Cre8or asked a question. $$anonymous$$y answer basically showed a working solution. It would seem the main problem in the first place is that she was missing

 using UIAddons;

from her uses directives.

avatar image cre8or · May 11, 2016 at 05:34 AM 0
Share

I appreciate the technical explanation, thank you, but to get it straight in my artist's brain... Would ^ or ! in English translate to "is not"? I'm sorry if I come off sounding a bit feeble, but sometimes I just need to break things down into simple terms. Thank you all very much for your patience and help.

avatar image elenzil cre8or · May 11, 2016 at 04:02 PM 0
Share

"foo ^= true" has the same exact effect as "foo = !foo", but the former would be read in english as "foo equals foo xor true", while the latter would be read as "foo equals not foo".

avatar image TBruce cre8or · May 11, 2016 at 04:17 PM 0
Share

They both essentially are the same and have the same result. Processor wise there is no real noticeable difference either especially with today's advanced processors, though ^ is slightly faster. Of course all programmers will have their preferences and will use what they know and are comfortable with.

Bitwise operations and bitwise math is an important part of computer program$$anonymous$$g and all programmers should understand them completely. Bitwise operation is a fast, primitive action directly supported by the processor, and is used to manipulate values for comparisons and calculations.

avatar image Owen-Reynolds cre8or · May 11, 2016 at 04:45 PM 0
Share

a=!a; is the standard way of flipping a variable between true and false. Just about every computer language does it that way. Any so-so programmer can easily read it. And, the not(!) symbol is also useful in other places -- if(!a) is a common shortcut for if(a==false).

Using ^= is much harder to read. Not obvious it's a true/false, most languages don't have it, and it's even an obscure symbol in C#. Not to imply anything about the Answerer, but the only use for ^= with a true/false is the "how many crazy ways can you do something" game.

avatar image TBruce Owen-Reynolds · May 11, 2016 at 06:28 PM 0
Share

I guess I might be showing my age a bit.

Show more comments
avatar image cre8or · May 20, 2016 at 07:59 PM 0
Share

@$$anonymous$$avina I have just gotten around to implementing this solution you have provided for me. I used this script in conjunction with another I wrote to control the answers in a quiz. Now, when the user chooses the incorrect answer in a true or false question, the correct answer will flash, providing subtle feedback that they've answered incorrectly. Now, the script I wrote is probably verbose and awkward, but it does accomplish what I intended it to do. Thank you very much for your help, I appreciate it!

avatar image TBruce cre8or · May 20, 2016 at 09:26 PM 0
Share

NP. Just glad I could help. Though the fix to your original code was mainly just adding

 using UIAddons;

to your uses statements, the other comments about using != being the norm in such a case is true. That was just a piece of script that I through together quickly and it was not meant to confuse you.

One thing to note though, if you ever have the need to toggle an integer between 1 and 0 you can not use != to do this because it does not work with numbers. A common method of toggling 1 and 0 is

 i ^= 1;
avatar image elenzil TBruce · May 20, 2016 at 09:34 PM 0
Share

to toggle between 1 and 0 for non-booleans, personally i prefer i = 1 - i. it's (again in my opinion) easier to understand, and it works for floats as well as ints which ^= does not.

avatar image
3

Answer by Jessespike · May 09, 2016 at 09:17 PM

Remove the argument. Only GetComponentsInChildren and GetComponentsInParent have the optional argument to include inactive GameObjects. GetComponent on it's own doesn't use any arguments.

 // Remove the "true" argument from GetComponent
 Q1TrueButton.GetComponent<BlinkEffect>();

Maybe you want to enable that component? In that case, it would look like this:

 Q1TrueButton.GetComponent<BlinkEffect>().enabled = true;
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 TBruce · May 09, 2016 at 09:32 PM 0
Share

@cre8or

@jesseike is correct. Please see the docs for more information on GetComponent.

avatar image cre8or TBruce · May 10, 2016 at 02:50 AM 0
Share

Thank you very kindly, @Jessespike @$$anonymous$$avina and @elenzil I appreciate all your answers, and surprisingly, this does make a bit of sense to me. However, here is my dilemma:

The Blink Effect is a .cs script that is inaccessible to me. It's part of the free Unity Asset called UIAddons. https://www.assetstore.unity3d.com/en/#!/content/24938

When I attach the BlinkEffect script, it places a bool in the inspector called IsBlinking, this can either be checked (on) or unchecked (off). If I omit the (true) parameter from Q1TrueButton.GetComponent();

and the IsBlinking is unchecked in the inspector, when the FALSE button is clicked, the TRUE button remains as it is. If IsBlinking is checked in the inspector, when the scene loads, the TRUE button is already blinking.

Is there a way to access the IsBlinking bool without being able to actually read the script that is driving that whole BlinkEffect function?

Basically, this is what I was trying to do by adding the parameter (true) in my original code.

Does anyone have any insight into that? Respectfully, $$anonymous$$imber

avatar image
0

Answer by elenzil · May 09, 2016 at 09:08 PM

this line:

 Q1TrueButton.GetComponent<BlinkEffect> (true);

should probably look something like this:

 Q1TrueButton.GetComponent<BlinkEffect>().someParameter = true;

.. the GetComponent() method returns a component (which is an object), and does not take any regular parameters. the isn't strictly a parameter; it's a generic/template specifier.

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 cre8or · May 10, 2016 at 02:54 AM 0
Share

@elenzil

Looking at your comment as to how the code should probably look, and taking into account what I have written above with regards to the IsBlinking bool, would it be a "safe" assumption that the line of code I'm looking for might be:

 Q1TrueButton.GetComponent<BlinkEffect>().IsBlinking = true;
avatar image elenzil cre8or · May 10, 2016 at 04:21 PM 0
Share

yep, precisely.

there's a lot of good discussion here.

to give yet another example, here's a slightly more verbose version of the line of code you wrote just above. the key part is that the GetComponent() call takes "BlinkEffect" as a 'template type' or 'generic type', but that's a separate concept from regular function parameters.

   BlinkEffect foo = Q1TrueButton.GetComponent<BlinkEffect>();
   foo.IsBlinking = true;

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

150 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

Related Questions

[HELP] Following the 2D character controller tutorial from the live training section (C#) 0 Answers

How can i pass a method like a parameter Unity3d C#? 3 Answers

tengo un problema con este script no puedo hacer que salte el personaje en un 2f,hi i have a problem making my chacter jump with this script 0 Answers

Null Reference in UnityStandardAssets.Utility.WaypointProgressTracker.Update 0 Answers

PostProcessingFactory.cs 2 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