Hello I have written this code
using UnityEngine;
public class PipeMiddleScript : MonoBehaviour
{
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
logic = GameObject.FindGameObjectWithTag("Logic").GetComponent<LogicScript>();
}
// Update is called once per frame
void Update()
{
}
private void OntriggerEnter2D(Collider2D collision)
{
logic.IncreaseScore();
Debug.Log("Score Increased");
}
And it is supposed to find the file after the object holding the tag “Logic” spawns but i can’t test the code as unity shows an error that it cannot find the object but it didn’t spawn yet so what am i supposed to do?
Why hasn’t it spawned yet? What code spawns it? You need to make sure that it is spawned before you try to access it.
You cannot disable an error, as errors indicate why a program cannot execute. You can only disable warnings. Errors must be fixed.
To resolve an error, you need to understand what it means and why it occurs. In this case, if Unity shows that it cannot find an object and you know this happens because the object hasn’t spawned yet, you have two options:
- Spawn the object earlier: This depends on how you’ve set up the spawning logic.
- Delay the search for the object: Use the
FindGameObjectWithTag expression after you’re certain the object has already been spawned.
There are different ways to implement either approach, but I’m not sure how you’re currently creating the “Logic” object.
The very first thing you need to do is stop making typing mistakes and start testing each piece of code as you get it into place.
For instance, this function:
Has been misspelled (capitalization) so Unity will silently ignore and NEVER call this method.
Hurry to the documentation to see the proper method declaration.
While you’re in the collision documentation, review all the other necessary prerequisites for receiving collision callbacks. If you miss one thing, it will not work.
At that point it’s probably a good idea to hop over to a basic tutorial for what you’re trying to do.
Two steps to tutorials and / or example code:
- do them perfectly, to the letter (zero typos, including punctuation and capitalization)
- stop and understand each step to understand what is going on.
If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.
Imphenzia: How Did I Learn To Make Games:
You could do a lazy loading thing here. “Logic” starts out as null so check for that condition in the beginning of OnTriggerEnter2D, and if it’s null, do the lookup there and assign the result. The next time the trigger fires, it won’t be null so you won’t have to do the lookup again.
Or you could invert the paradigm, your PipeMiddle has a dependency on “Logic” so you could have a separate script that starts out instantiated and has a method (e.g. NotifyLogicCreated) that raises an event. When PipeMiddle starts, it subscribes to the event. When Logic is created, it calls the method that raises the event. The event handler in PipeMiddle would then set the logic variable.