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
Answer by TBruce · May 10, 2016 at 03:30 AM
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;
}
}
}
@$$anonymous$$avina I apologize for my ignorance, but I am just learning: I understand some operators, for example: == is equivalent to != is not equal
=|| or && and % modal ... Can be used to describe time But I have never seen the "little hat" used before. What does ^= mean?is greater than or equal to
^ 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.
Thank you very kindly @$$anonymous$$avina for that link.
blinkEffect.isBlinking = !blinkEffect.isBlinking;
would achieve the same result.
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.
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.
"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".
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.
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.
@$$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!
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;
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.
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;
@jesseike is correct. Please see the docs for more information on GetComponent.
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
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.
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;
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;