Trying to enable/disable sprite of a different object
Im trying to enable the sprite of another object here, but for some reason the code just breaks if I add the attack1Sprite.attack1Sprite.enabled = true; (I just named them the same). the entire code after that stops working

40 Replies
also tried enabling and disabling the sprite a method in the other script and calling that method instead but same problem
You will have to be more clear and share the exception that happens and causes the code to break. Additionally, $paste the whole code in here so it's clear how it works.
If your code is too long, you can post to https://paste.mod.gg/, save, and copy the link into chat for others to see your shared code!
Right now this is a lot of guesswork and it sounds like you might have a NRE. I want to be sure, though.
alr, though no error is actually showing up
just that it stops working
Then share the code
public class groundAttack : MonoBehaviour
{
private attack1_Sprite attack1Sprite;
private SpriteRenderer sprite;
private BoxCollider2D trigger;
attackCaller attackCaller;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
attack1Sprite = GetComponent<attack1_Sprite>();
sprite = GetComponent<SpriteRenderer> ();
trigger = GetComponent<BoxCollider2D> ();
attackCaller = GameObject.FindGameObjectWithTag("attackCaller").GetComponent<attackCaller>();
}
private float time = 0.0f;
[SerializeField] public float warn = 0.75f;
// Update is called once per frame
void Update()
{
//if script is called
} public void attack1() { time += Time.deltaTime; sprite.enabled = true; if (time >= warn) { attack1Sprite.attack1Sprite.enabled = true; trigger.enabled = true; if (time >= 0.75) { attack1Sprite.attack1Sprite.enabled = false; trigger.enabled = false; time = 0.0f; sprite.enabled = false; attackCaller.cooldown = 0.0f; attackCaller.attackChosen = false; } } }
} public void attack1() { time += Time.deltaTime; sprite.enabled = true; if (time >= warn) { attack1Sprite.attack1Sprite.enabled = true; trigger.enabled = true; if (time >= 0.75) { attack1Sprite.attack1Sprite.enabled = false; trigger.enabled = false; time = 0.0f; sprite.enabled = false; attackCaller.cooldown = 0.0f; attackCaller.attackChosen = false; } } }
Please read the bot message and share the code properly
oki
https://paste.mod.gg/zuktwfzzympd/0
https://paste.mod.gg/wjyrqznotmqd/0 - everything in the other script (basically nothing)
BlazeBin - zuktwfzzympd
A tool for sharing your source code with the world!
BlazeBin - wjyrqznotmqd
A tool for sharing your source code with the world!
So where is
attack1()
called?
Not in the code you shared, at leastBlazeBin - meegrpionpxv
A tool for sharing your source code with the world!
I find it hard to believe the code just stops
I suggest you use breakpoints and navigate through the code. Set a break point at the start of
attack1()
and step through it to see what the behaviour is and where it goes
My guess, it doesn't do what you think it does and the code is simply skipped🤔 Unity, Huh, How?
The debugger
The debugger is a tool that halts code execution. Values can be inspected, lines of code can be stepped over, and sometimes even modified. This does not require recompiling your code, or exiting Play Mode.
the code works completely fine if I remove this

ok
If a line of code causes the code to stop working, it would throw an exception and show in the console
From your messages I conclude this does not happen
ok I clearly was too vauge on what happens
See what the debugger does
Set a breakpoint, step through it. If something happens it will inform you.
FYI I am aware of the issue and what you did wrong, but this is basic debugging and you should get used to it. If you prefer to just get the solution then I can share that as well but you will end up with less experience.
alr ill try debuggin myself
See what the debugger does and what values it gives to you. You'll see a value ends up being
null
which is unexpected.uhh ok so I set up the breakpoint now how do I step through the code :/
Does it pause the code when the method is called?
It should
yep
F10 or press this icon


ok yea I have no 0 idea whats happening :b_confused:
when stepping through it
Stepping through means it goes to to the next line being invoked, and it shows you the state
The thing you can check is if the variables are what you expect it to be
Tech With Pat
YouTube
How to Debug in Visual Studio: A Beginner's Guide
In this video, I'll show you how to debug your code in Visual Studio. I'll cover the basics of code debugging, and help you get started debugging your C# code!
Subscribe for more content like this: https://www.youtube.com/c/TechWithPat?sub_confirmation=1
Support the channel: https://www.buymeacoffee.com/itsmepatrick
TIMESTAMPS :
00:00 Introdu...
so when stepping after the line it just goes back to the start of the method and keeps loopin
huh
What line?

I got the code workin by making it find the object like this :/

still dont know what this means though
Did you compile the code at all? It should not randomly stop at that point
Also, are you certain that there are no errors in the Unity console when you run the code?
theres this but its completely unrelated to this problem (and been there for a lot longer)

ok since its workin now, what was debuggin supposed to show me ðŸ˜
Except it is entirely related
Your code can stop because of this
Debugging is a very easy way to step through code and view its behavior without having to place a lot of print statements and everything
So is it working now? What changed?


Idk what to tell you
It's very hard to discuss this issue with you
I suggest you validate code is executed, check how it's executed. Validate there are absolutely no errors
I highly doubt there are no errors if your code compiles and runs