Class not working properly

so i have this class:

[System.Serializable]
public class craft_objects{
    public int quantity;
    public GameObject required_object;
}

and when i check the trigger

    void OnTriggerEnter(Collider craft)
    {
        for (int i = 0; i < Required_objects_to_craft.Length; i++) {
            if (craft.gameObject.name.Replace ("(Clone)", "") == Required_objects_to_craft [i].required_object.gameObject.name) {
Print("YES");
                return;
            }
        }
    }

the problem is that is printing every time “YES” 2 times , doesn’t matter the size of the array , every time it print “YES” 2 times.
Please help :confused:

If the “Required_objects_to_craft” array is 0 in length then it can’t possibly execute that loop. So you can’t be right that the size of the array doesn’t matter.

ok but how can i fix it?

There’s a return immediately after printing YES. Is that intentional?

Either way, that’s going to make the array size irrelevant if it immediately returns after “YES” is printed.

Write some print (aka. Debug.Log) statements, and check which indexes are giving the “Yes” and what values are being compared (eg: names)… Just to peek at what’s happening.

Yes, like maybe this script is running twice or something.

without the return it does exactly the same thing :frowning: printing 2 times…i also tried with break but its the same

In that case, you’re going to need to set a breakpoint to see what exactly is going on.

Or add a Debug.Log to print the following values: Size of array, first string, and second string.

Also, the string compare could be done like this instead (may or may not be related to your issue, this is just advice):

craft.gameObject.name.Contains(Required_objects_to_craft [i].required_object.gameObject.name)

‘Contains’ could return the wrong result, maybe you meant Compare?

If it’s printing twice, it’s triggering twice. If you have one object in the array or 20 in the array but the first object matches, then it’s going to print once(then the return triggers, thus your array size doesn’t matter). But it will print once for each time the trigger activates. So, my guess is you have more than one thing activating the trigger.

I would print out what is triggering your trigger to see if multiple colliders are triggering it. I don’t know the setup of your game, but for example if you have a player with two colliders, those colliders would both trigger it.

And has also been suggested, see what index it’s on, what the object is, etc. You need to do more print/debug.log statements to understand what is going on.

Can you please replicate my code in a unity project?? i will be glad if you do, im just curious to see if you wil have the same issue

both objects have “is trigger” enabeld, maybe thats why?

I don’t think anyone is going to replicate your project, sorry to say.

You should read what he wrote in response… he gave a very good explanation, along with suggestions for fixes/etc…

ok thanks anyway

So, did you try what’s been suggested? :slight_smile:

If I’m reading what he’s trying to do correctly, he’s trying to strip out “(Clone)” from the name in order to do a direct string comparison.

My suggestion is that he doesn’t need to strip out the clone text, since a partial match could suffice.

It’s either that, or use string.StartsWith. That may be the better choice, actually.

ok thanks for info :wink:

Yes, I get what you’re saying & it makes sense. I brought it up (in either case, ‘contains’ or ‘startswith’) could have an issue if you have words that… contain or start with the same characters as other words, but aren’t the same :slight_smile: