C
C#2y ago
Istix

❔ cs0670 unity 2d (very simple)

so basically, i was bored and decided to just make a "game" and yeah its weird. but basically i have a circle that spins infinitely that im trying to get to "if rotation = 310* stop" basically making a wheel of fortune or whatever. and my pc doenst have enough space to make a 180 frame photoshop animation (trust me, i would, i was really bored) and i just need to know what is wrong with the code, i keep getting cs0670 "field cannot have void type", lmk if u need more info. Code below (please dont judge i know, im bad at coding, i do it for school)
using System.Collections; using System.Collections.Generic; using UnityEngine; public class SpinGray : MonoBehaviour { public static void Stop() { transform.Rotate(new Vector3(0, 0, 320)); } // Start is called before the first frame update void Start() {
} // Update is called once per frame void Update() { transform.Rotate(new Vector3(0, 0, 120 * Time.deltaTime)); if (transform.Rotate(new Vector3(0,0,319))) { Stop; } } }
59 Replies
Istix
IstixOP2y ago
the basic "keep spinning" script on the circle is just void Update() { transform.Rotate(new Vector3(0, 0, 120 * Time.deltaTime)); } oh fun now my rotate script dont work anymore either worked perfectly a couple minutes ago and didnt touch it.
TheRanger
TheRanger2y ago
check the debug log there could be some error messages printed
Istix
IstixOP2y ago
i got this that i got when i tried new thing, tried something simpler earlier and only got 0670
rick_o_max
rick_o_max2y ago
Transform.Rotate does not return you anything That is why you have the voiderror
TheRanger
TheRanger2y ago
which file/ line number throws that?
rick_o_max
rick_o_max2y ago
if (transform.Rotate(new Vector3(0,0,319)))
if (transform.Rotate(new Vector3(0,0,319)))
here
Istix
IstixOP2y ago
rick_o_max
rick_o_max2y ago
Also
TheRanger
TheRanger2y ago
can you show that line?
rick_o_max
rick_o_max2y ago
Don't check for float values like that
Istix
IstixOP2y ago
9 or all of em
TheRanger
TheRanger2y ago
lets go with 9 first which line is 9?
Istix
IstixOP2y ago
sorray
Istix
IstixOP2y ago
rick_o_max
rick_o_max2y ago
Your if wants to get a boolean from a void method. transform.Rotate does not returns anything, it is void
TheRanger
TheRanger2y ago
first
rick_o_max
rick_o_max2y ago
Stop must be called as a method: Stop();
TheRanger
TheRanger2y ago
why did you set ur Stop method as static?
Istix
IstixOP2y ago
idfk, some dude on the internet
TheRanger
TheRanger2y ago
i suggest you learn the basics of C# before you start unity, or ur gonna have a tough time check links below $helloworld
TheRanger
TheRanger2y ago
dont just write random stuff and expect things to work magically, u have to know what u are doing
Istix
IstixOP2y ago
fixed one error, i am tired and stupid, ty
rick_o_max
rick_o_max2y ago
The transform.Rotate simply rotates the object
Istix
IstixOP2y ago
yeah
rick_o_max
rick_o_max2y ago
It won't return you anthing you could use to check the object rotation
Istix
IstixOP2y ago
yeah i know, ive been trying to figure it out and just put that and it has stayed and i havent changed it
rick_o_max
rick_o_max2y ago
You can check it by the EulerAngles, tho, which could be asier
Istix
IstixOP2y ago
havent gotten that far but yeah, as i said, im pretty bad at coding and wont have big use of it as im switching schools, as i said, im just bored and have nothing to do
rick_o_max
rick_o_max2y ago
private float spinThreshold;
private float minRange;
private float maxRange;
private void Stop() {
Debug.Log("Stop!");
}
private void Start() {
spinThreshold = 10f; //a value to make sure you wont overpass the stop point, could happen depending on your rotation speed
minRange = Random.Range(0f,360f); //a random stop angle in degrees (it is a roulette, right?), you can use a simple angle here as well
maxRange = Mathf.Repeat(minRange + spinThreshold, 360f); //the spinThreshold applied to the minRange, the 360f value is used to wrap the degrees when it reaches 360f, so it goes back to 0f and visce-versa
}
private void Update() {
//checks if your rotation euler angles is between minRange and maxRange
if (transform.eulerAngles.z > minRange && transform.eulerAngles.z < maxRange) {
//Here is where your wheel stop, you can call the Stop method to show a message, etc
Stop();
} else {
//If your wheel is not in the stop range, continue rotating it
transform.Rotate(new Vector3(0f, 0f, 120f * Time.deltaTime)); //rotates your object at 120f degrees per second in the z axis. Multiplying by the Time.deltaTime makes the speed be relative to one second
}
}
private float spinThreshold;
private float minRange;
private float maxRange;
private void Stop() {
Debug.Log("Stop!");
}
private void Start() {
spinThreshold = 10f; //a value to make sure you wont overpass the stop point, could happen depending on your rotation speed
minRange = Random.Range(0f,360f); //a random stop angle in degrees (it is a roulette, right?), you can use a simple angle here as well
maxRange = Mathf.Repeat(minRange + spinThreshold, 360f); //the spinThreshold applied to the minRange, the 360f value is used to wrap the degrees when it reaches 360f, so it goes back to 0f and visce-versa
}
private void Update() {
//checks if your rotation euler angles is between minRange and maxRange
if (transform.eulerAngles.z > minRange && transform.eulerAngles.z < maxRange) {
//Here is where your wheel stop, you can call the Stop method to show a message, etc
Stop();
} else {
//If your wheel is not in the stop range, continue rotating it
transform.Rotate(new Vector3(0f, 0f, 120f * Time.deltaTime)); //rotates your object at 120f degrees per second in the z axis. Multiplying by the Time.deltaTime makes the speed be relative to one second
}
}
Also, you don't have to make your Stop function static. You can remove the static keyword
Istix
IstixOP2y ago
makes decent sense
rick_o_max
rick_o_max2y ago
Wait
Istix
IstixOP2y ago
ohkey just tried some stuff i thought made decent sense, and only get one error now, 0029 void to bool. probably my worst side, still cant get into my head what words give what
rick_o_max
rick_o_max2y ago
Try using my code Read the comments
Anton
Anton2y ago
yeesh, spoon-feeding. let them learn the fundamentals at their own pace
rick_o_max
rick_o_max2y ago
I see no problem. I've been spoon-feeded thousand of times when I was learning :V But read the comments @Istix so you can start making some idea of what the code is doing
Istix
IstixOP2y ago
readin checked my lesson planning as well, learning about eulerangles in 2 weeks : >
rick_o_max
rick_o_max2y ago
If you're on Visual Studio, hover the mouse over a method, like the Rotate, if you see void in the beginning of the method declaration, it does not return anything, so you can't use this method result inside an If
rick_o_max
rick_o_max2y ago
Like this one, a void, it does not return anything
rick_o_max
rick_o_max2y ago
(Took this from my code, just an example)
Istix
IstixOP2y ago
the fact that i understand more from u people telling me stuff than i do from my teacher must mean something
rick_o_max
rick_o_max2y ago
University teaching methods sucks Ppl still teaching C/CPP here where I live as the programming basis
Istix
IstixOP2y ago
university O.O im in high school xd
rick_o_max
rick_o_max2y ago
Universities here (Brazil) are no different
Istix
IstixOP2y ago
swedish school is special ah
rick_o_max
rick_o_max2y ago
Don't get upset bc you don't understand stuff right now, you'll surely become a good programmer if you focus on it
Istix
IstixOP2y ago
noone is probably stupid enough to try same thing as me and get the same error. but just letting u know, u had theshold on 2 thingymajigs and threshold on the private float
rick_o_max
rick_o_max2y ago
Typo, tks
Istix
IstixOP2y ago
im swapping from programming to mechanic cuz im still tryna figure out what to do with my life, i liked programming for a bit but imo its just fun when u get those kicks like "i wanna do this"
rick_o_max
rick_o_max2y ago
Then my job here is done...lol
Istix
IstixOP2y ago
and also, just looking at the logs and stuff, im guessing the "minValue" is supposed to be minRange? cuz currently its just a bunch of does not exist in current context
rick_o_max
rick_o_max2y ago
Yah, it was just superfast typing, I'm about to go
Istix
IstixOP2y ago
name minrange name maxrange well thank u a lot for all your help, i appreciate it i had a really dumb way of doing it damn was gonna do like "if button is clicked then randomize a number between 1 and 100, if the number is 50-100 its a common reward, etc etc" and damn, this seems a LOT smarter
rick_o_max
rick_o_max2y ago
You can do that, there are better ways to do that on Unity, like Coroutines, you could take a look, but I guess you should learn a bit more about the fundamentals before it
Istix
IstixOP2y ago
ok so quick update unity is screaming at me
Istix
IstixOP2y ago
so i changed this to 90. literally nothing else.
Istix
IstixOP2y ago
Istix
IstixOP2y ago
script class cannot be found.... im so annoyed and magic. it works
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server