I need help with my code - Noob

Good day,
Please me kind , I am still very new. That being said if you do give an answer please explain it in a very simple way.
Okay,

I have a main character that runs around. In the world there will be fireflies that you can jump through and collect stuff. This is triggered by OnTriggerEnter. In my code , it looks for the right tag and then activates that method. My problem is that all the fireflies have the same tag so when I jump through one all of them gets triggered. So I need a way to say - get the name of the object and call the method in that objects code.

or if there is a more simple way, I am open

Thank you!

public class Collectable : MonoBehaviour
{
    thefirefly neededScript;
  
   

    void OnTriggerEnter(Collider other)

        {
            if (other.gameObject.tag == "Collectable")
            GameObject.Find
            {
             neededScript = GameObject.FindGameObjectWithTag("Collectable").GetComponent<thefirefly>();
             neededScript.FireflyMethod();
            }

     


    }
   
  
}

Your provided code won’t even compile due to line 11. There seems to be a flawed idea with the naming conventions. Currently, your script is named “Collectable”, yet within the OnTriggerEnter, you first check for a tag “Collectable” and then again search for the first object with that tag… it’s confusing. If other.gameObject.tag == “Collectable”, there’s no need to search again for an object with the same tag (line 13). Simply extract the necessary component (thefirefly) from other.gameObject and call its method.

Alternatively, please provide a more specific explanation of your entire situation. Where is this code located? What tags are used on which objects? Why is the “Collectable” object also a firefly if you’re only supposed to jump over them?

Thank you for your reply. So my main character is called Ruby, she has this script attached. You jump through the fireflies where they then explode and you collect their orbs.So my fireflies have the tag Collectable.

So the idea is that in this world there will be 100s of fireflies with the tag “Collectable”,but with my current method when I jump through 1 firefy all 100 will die because it activates the same method. So my idea is that rather then looking for the tag “Collectable” maybe I should rather have something that says - Find the gameobject name you just jumped through and activate that specific gameobjects method.

Hope this makes sense

Are you saying that line 13 is completely unnecessary?
I can see that its called twice but I have no idea on how to rewrite( I am still learning and 75% of the code I copy)

Thank you

so, the trigger tells you which firefly thing you hit. You should be using that rather than finding the first…

Such as

void OnTriggerEnter(Collider other)
        {
            if (other.gameObject.tag == "Collectable")
            GameObject.Find
            {
             neededScript = other.gameObject..GetComponent<thefirefly>();
             neededScript.FireflyMethod();
            }

    }

although your script doesnt scream doing it for all fireflies unless the fireflymethod does it all :stuck_out_tongue: but finding an object may not mean it used the one you hit…

1 Like

Yes, now your situation is becoming clearer than before, thank you. So theoretically, you’re almost there, but you can simplify the code. For example, like this:

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.tag == "Collectable")
    {
        other.GetComponent<thefirefly>().FireflyMethod();
    }

    // or
    if (other.TryGetComponent(out thefirefly firefly))
    {
        firefly.FireflyMethod();
    }
}

Also, please show what’s inside the FireflyMethod() method; perhaps there’s an issue there too, like it’s somehow deleting all fireflies within itself.


What’s written below is not valid C# syntax; it will cause an error and is generally inappropriate in the context of the task:

GameObject.Find
{
    // stuff
}
1 Like

Thank you ,So with the Method, the idea is that when I jump through a firefly, it gets inactive and orbs fall out( not sure if there is an easier way to instantiate a multiple of the same prefab, but for now this is what I did. After the firefly gets inactive it get activated again in 3 seconds.

here is the Method

public class thefirefly : MonoBehaviour
{
    public GameObject fireFly;
    public GameObject activeGameObject;
    public GameObject orbPrefabGreen;
    public GameObject orbPrefabPurple;

    public void FireflyMethod()
    {
        Instantiate(orbPrefabGreen, transform.position, Quaternion.identity);
        Instantiate(orbPrefabPurple, transform.position, Quaternion.identity);
        Instantiate(orbPrefabGreen, transform.position, Quaternion.identity);
        Instantiate(orbPrefabPurple, transform.position, Quaternion.identity);
        Instantiate(orbPrefabGreen, transform.position, Quaternion.identity);
        Instantiate(orbPrefabPurple, transform.position, Quaternion.identity);
        activeGameObject.SetActive(false);
        Invoke("SpawnDelay", 3);



       


    }
       private void SpawnDelay()
      {
        fireFly.SetActive(true);
      }



}

I’m really starting to get confused. Why are you deactivating one object

activeGameObject.SetActive(false);

but activating another fireFly.SetActive(true);?

Im so sorry for my confusing code.

The idea is to just SetActive false to firefly and then after 3 seconds setActive true to the same firefly

but you never set firefly to anything

1 Like

Most likely, this kind of copying and pasting is hindering development because in your seemingly straightforward task, the code appears to be composed of fragments that “kind of fit.” But in reality, they disrupt the program’s logic.

I think that might be my main question. If I can somehow say - Main character jumped through firefly, then this code should setActive false to that specific firefly it jumped through. then setactive true 3 seconds later.

I understand. This is my second question on unity forum and Im starting to realise that it is very helpful in helping me understand where I am messing up.

Perhaps you’ve already done this, but simply add a long trigger extending from the top of the firefly up into the sky so that when jumping over the firefly, it’s impossible not to trigger it.

The trigger isnt the problem. I manage to jump into the trigger every time. Its just that when I trigger the firefly I jump through all the others also get triggered

There seems to be something misconfigured with the object itself (or the prefabs), references, and so on. It needs to be investigated in more detail. If your game is still relatively small and you can package it into a small Assets - ExportPackage, it might help to identify the cause of the problem more quickly. Guessing will take a long time and be difficult, and posting a stack of screenshots of all the object settings is very labor-intensive, and also not very desirable to review.

1 Like

Do you think I can swap things around. So rat

Thank you, I do think if I give it another go I might be able to do it more simplified, thanks to your help. I guess in short all that needs to happen is that when ever the main character runs into a firefly, that specific firefly needs to die…I think if I don’t connect everything to the tag and rather work on the specific firefly she ran into it would be more simple.