C
C#•2d ago
Axivex

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
No description
40 Replies
Axivex
AxivexOP•2d ago
also tried enabling and disabling the sprite a method in the other script and calling that method instead but same problem
FusedQyou
FusedQyou•2d ago
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.
MODiX
MODiX•2d ago
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!
FusedQyou
FusedQyou•2d ago
Right now this is a lot of guesswork and it sounds like you might have a NRE. I want to be sure, though.
Axivex
AxivexOP•2d ago
alr, though no error is actually showing up just that it stops working
FusedQyou
FusedQyou•2d ago
Then share the code
Axivex
AxivexOP•2d ago
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; } } }
FusedQyou
FusedQyou•2d ago
Please read the bot message and share the code properly
Axivex
AxivexOP•2d ago
oki
Axivex
AxivexOP•2d ago
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!
FusedQyou
FusedQyou•2d ago
So where is attack1() called? Not in the code you shared, at least
Axivex
AxivexOP•2d ago
BlazeBin - meegrpionpxv
A tool for sharing your source code with the world!
FusedQyou
FusedQyou•2d ago
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
FusedQyou
FusedQyou•2d ago
🤔 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.
Axivex
AxivexOP•2d ago
the code works completely fine if I remove this
No description
Axivex
AxivexOP•2d ago
ok
FusedQyou
FusedQyou•2d ago
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
Axivex
AxivexOP•2d ago
ok I clearly was too vauge on what happens
FusedQyou
FusedQyou•2d ago
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.
Axivex
AxivexOP•2d ago
alr ill try debuggin myself
FusedQyou
FusedQyou•2d ago
See what the debugger does and what values it gives to you. You'll see a value ends up being null which is unexpected.
Axivex
AxivexOP•2d ago
uhh ok so I set up the breakpoint now how do I step through the code :/
FusedQyou
FusedQyou•2d ago
Does it pause the code when the method is called? It should
Axivex
AxivexOP•2d ago
yep
FusedQyou
FusedQyou•2d ago
F10 or press this icon
No description
FusedQyou
FusedQyou•2d ago
No description
Axivex
AxivexOP•2d ago
ok yea I have no 0 idea whats happening :b_confused: when stepping through it
FusedQyou
FusedQyou•2d ago
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
FusedQyou
FusedQyou•2d ago
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...
Axivex
AxivexOP•2d ago
so when stepping after the line it just goes back to the start of the method and keeps loopin huh
FusedQyou
FusedQyou•2d ago
What line?
Axivex
AxivexOP•2d ago
No description
Axivex
AxivexOP•2d ago
I got the code workin by making it find the object like this :/
No description
Axivex
AxivexOP•2d ago
still dont know what this means though
FusedQyou
FusedQyou•2d ago
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?
Axivex
AxivexOP•2d ago
theres this but its completely unrelated to this problem (and been there for a lot longer)
No description
Axivex
AxivexOP•2d ago
ok since its workin now, what was debuggin supposed to show me 😭
FusedQyou
FusedQyou•2d ago
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?
Axivex
AxivexOP•2d ago
No description
No description
FusedQyou
FusedQyou•21h ago
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

Did you find this page helpful?