#okay so like this ```public class
1 messages Β· Page 1 of 1 (latest)
IEnumerator SpawnSpd()
{
while(isCollected == false)
{
yield return new WaitForSeconds(wait);
Instantiate(speedPWRUP, new Vector3(Random.Range(-10, 10), 10, 0), Quaternion.identity);
}
}
oop
i just did that
{
public float wait;
public bool collected;
public GameObject speedPWRUP;
public PlayerMovement playerMovement;
public void Start()
{
collected = false;
wait = (20);
StartCoroutine(SpawnSpd());
}
IEnumerator SpawnSpd()
{
while (collected == false)
{
yield return new WaitForSeconds(wait);
Instantiate(speedPWRUP, new Vector3(Random.Range(-10, 10), 10, 0), Quaternion.identity);
if (collected == true)
{
StopCoroutine(SpawnSpd());
playerMovement.movementSpeed = 7;
}
}
}
}```
i was gonna ask
alr lemme see if it works
That is not how you stop a coroutine https://unity.huh.how/programming/coroutines/stopcoroutine
sagudhiasd
yield break will exit a coroutine
Will it not exit itself if the while evaluate false ?
(i'm talking about my answer to them)
so just without the stopCoroutine?
IEnumerator SpawnSpd()
{
while (isCollected == false)
{
yield return new WaitForSeconds(wait);
Instantiate(speedPWRUP, new Vector3(Random.Range(-10, 10), 10, 0), Quaternion.identity);
}
playerMovement.movementSpeed = 7;
}
forgot 1 line in my previous answer
Yeah, I made a mistake in my first answer and only handle the spawn part.
As jody said, you have a to add the speed part right after exiting the while loop
could it in theory be something wrong in playerMovement as well?
alr it doesnt print too
is there anything wrong with this? ``` public void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Player")
{
speedPWRUP.collected = true;
Destroy(this.gameObject);
}
}
}
let me try to make a function in playermovement actually
I am learning too, do not take my suggestions...more so me asking questions about the code.
π its quite hard but i aint never gon give up
What doesn't print ?
Where is this located? Is it in the same script where that other stuff provided was put? If so, can you put the whole code
that one is in the actualt speedPWRUP script, the other one is the spawning script of speedPWRUP
i can show u both if u want
I guess my confusion, again as a new programmer, is where are you calling anything to check from there. I see you set a bool and destroy the object, but nothing to run against the bool
I think I know what's the problem is if you have this in your code.
You're destroying the object before it modifies your player speed
yes
never ever ever compare tag like this π€―
just transform.CompareTag("tag")
Try Destroy(this.gameObject, 1f)
It won't hide the object, but we'll know if it's the issue
== is bad π‘
nope it aint workin
Actually, even this wouldn't be enough since you could be in a frame where the coroutine is waiting and it wouldn't trigger (i mean my Destroy(x, time) )
this is working, it is not working in your code β
Forget about it, there're cases it won't work
its gameObject
also this is redundant
yes paleese
gameObject is already this.gameObject
this is the reference of the script
you don't have ot use this in your script unless you have multiple variables with the same name
We're trying to figure the issue without trying to touch the code too much. Better/right/whatever is not on the plate here
yeah, I just don't know what the issue is, cuz I have left the room before
The coroutine is on the spawner, which also holds the collected. The design is quite flawed actually ^^'
anyway, you just should provide correct code form the first time, that's how beginners will learn faster π
you're right
The spawner should keep reference of what it spawn at least, no ?
Or the spawned object have its own logic
can you give me the sentence where they say what doesn't work, please?
anyway this code is redundant
I have mentioned the easiest way to do it in #π»βcode-beginner some time ago
I think you just have to provide all scripts that are somehow connected with your spawning
preudo-pseudocode is great, but I really think they have to show their full code
method
it's a prefab, yes
I think we're missing speedPWRUP in the thread so here's their initial code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpeedPWRUP : MonoBehaviour
{
private Vector2 screenBounds;
public int randomScore;
public PlayerMovement playerMovement;
public SpawnSpeedPWRUP speedPWRUP;
void Start()
{
screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
}
void Update()
{
if (transform.position.y < screenBounds.y * 2)
{
Destroy(this.gameObject);
}
}
public void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Player")
{
speedPWRUP.collected = true;
Destroy(this.gameObject);
}
}
}
"gameObject"
I just c/c their code, stop with that xD
and I really don't get why you have to use unnecessary collected if it's just used within that coroutine
why don't you stop the coroutine a that trigger method
my head is going to explode π
They'd need a ref to the coroutine in a script that doesn't have ownership on the coroutine
π€£
they need a reference for that class to access collected boolean
so that doesn't really make a lot of sense π
they just can access that class with coroutine
and start it
which reference they need to have a reference to the coroutine?
can anybody just tell me what to do and explain why i need to do that
@signal void and coroutine cannot be static
we've been going on with this for like an hour
I think that's your problem here
please, just give 2 scripts
!code
π Large Code Blocks
Large code blocks should be posted as links to services like:
https://gdl.space/, https://paste.ofcode.org/, https://hatebin.com/
https://paste.myst.rs/, https://hastebin.com/
π Inline Code
Surround code with three backquotes. Not quotation marks.
To get C# formatting the first line should only contain cs or csharp.
Add a comment with a line number if there is an error message.
```cs
// Your code here
```
Do not share screenshots of code unless requested.
yeah it worked, i tried it before
you're trying to teach them without even having their scripts they should have changed by that time
i referenced a function in the if statement to make the movementspeed go to 7, it printed but didnt actually change the speed
@placid condor it seems like you're trying to make them solve your issue, but don't have any wish to take part in solving it
oh, really? I am really happy you have understood that you at least need to see their code in order to help them
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using UnityEngine;
public class SpawnSpeedPWRUP : MonoBehaviour
{
public float wait;
public bool collected;
public GameObject speedPWRUP;
public PlayerMovement playerMovement;
public void Start()
{
collected = false;
wait = (5);
StartCoroutine(SpawnSpd());
}
IEnumerator SpawnSpd()
{
while (collected == false)
{
yield return new WaitForSeconds(wait);
Instantiate(speedPWRUP, new Vector3(Random.Range(-10, 10), 10, 0), Quaternion.identity);
}
}
}```
```using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpeedPWRUP : MonoBehaviour
{
private Vector2 screenBounds;
public int randomScore;
public PlayerMovement playerMovement;
public SpawnSpeedPWRUP speedPWRUP;
void Start()
{
screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
}
void Update()
{
if (transform.position.y < screenBounds.y * 2)
{
Destroy(this.gameObject);
}
}
public void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Player")
{
speedPWRUP.collected = true;
playerMovement.AddSPD();
Destroy(gameObject);
}
}
}```
yeah
no they keep spawning
{
Debug.Log("cock");
movementSpeed = 7;
}``` this?
yes
but speed is not being changed
fuck man, where have I even mentioned you to do it
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float movementSpeed = 6;
public float inputHorizontal;
public float inputVertical;
Rigidbody2D rb2D;
public void Start()
{
rb2D = GetComponent<Rigidbody2D>();
}
public void Update()
{
inputHorizontal = Input.GetAxisRaw("Horizontal");
}
public void AddSPD()
{
Debug.Log("cock");
movementSpeed = 7;
}
void FixedUpdate()
{
if (inputHorizontal != 0)
{
rb2D.velocity = new Vector2(inputHorizontal * movementSpeed, inputVertical);
}
else rb2D.velocity = new Vector2(0f, 0f);
}
}
this code doesn't seem to be wrong unless trigger method just isn't called
The trigger is called, they have the log in their console
what's even the problem?
movementspeed aint changing
is method called?
huh
good for you
sorry whats a method again i forgot π
Yes, the log is appearing in their console according to their response to jody
yeah its printing
are you asking me what's the name of your method?
you checked?
the method where movement speed is changed
yes
because to me it seems like movementSpeed should have been changing
theres no reason for it not to be changing
do you have other scripts?
^^^ the only reason movementSpeed would still be 6 is if its being set to 6 constantly by something else
or in the inspector
They use it in fixedUpdate, so the modification should be visual enough between 6 and 50.
i was checking it throught the inspector before ill try update now
But you're right, something is modifying this value elsewhere
only explanation
thats literally what we're saying
your 3 possibilities are:
- its being changed and you dont realize
- its being changed back to default value by something without you knowing
- its actually not being changed and you are being confused by an identical Debug.Log() elsewhere
that's not that hard for them to answr
If you're using Visual Studio, right-click your variable and click on "find all references"
and thats what we're waiting on
just don't suggest anything until they answer you
please
- i checked through inspector and debuglog
- ill check rn
im literally not
might be worth a restart, not that i think thats the issue
The only script that calls AddSPD is SpeedPWRUP, right ?
might not
yah
uh sure ig but it does run at like 30 fps for sum reason π
and? just export it, please
in zip format
it says build completed with the result of "failed" π
^^ maybe a restart will do good then
exporting package? really?
nah
π€£
Ok ! Have a nice evening and don't hesitate to reach out when you want to pursue the debugging π
it's kinda you haven't even bother yourself to export the package
that's either laziness or disrespect I mean
anyway
OK. I'll try to stay polite here, but even if you're frustrated, try to remain respectful and not assume they are lazy. They're on this problem for a few hours now, it's alright if they want to give up for now.
Taking a break won't hurt
you have to respect your time my man
break won't hurt for you, but I mean
like someone is solving my problem for quite a long time, I really won't think about making a break
also when somebody is waiting for my response
maybe it's just about me
also I will try to reply everyone who is trying to help me
Yes, I agree. But we're also not going anywhere here. So i prefer having someone admit they're lost and their brain is not registering anything anymore. Anyway, that's not the place to talk about this.