0

I've been struggling with a method that needs to check, and then change a variable that is from a separate script and object. I'm trying to check an array for entries that are not null, and entries that are not already used in my placeholders. When both of these are true, it's supposed to copy the contents of that array into my actualReadIn string variable, which is held in another script. Categories is the string array I'm reading from.

Here is the code I have:

int checkForNotNull(string[] a)
    {
        for (int x = 0; x < a.Length; x++)
        {
            if (a[x]!= "" && orbScript1.actualReadIn != a[x] && orbScript2.actualReadIn != a[x] && orbScript3.actualReadIn != a[x] && orbScript4.actualReadIn != a[x]
                && orbScript5.actualReadIn != a[x] && orbScript6.actualReadIn != a[x]&& orbScript7.actualReadIn != a[x] && orbScript8.actualReadIn != a[x])
                //^^ This should both check to see if the entry is not nothing, and see if the entry matches any others orbs.

                return x;
        }
        return -1;
    }

^^ This code checks to ensure that the other placeholders don't already contain the array entry


void readArray(string actualRead)
    {
        if (taskPhase == 1)
        {
            if (actualRead == "")
            {
                actualRead = Categories[checkForNotNull(Categories)];
            }
        }

        if (taskPhase == 2)
        {

        }
    }

^^ The argument on this script is supposed to be orbScript1.actualReadIn. Which is a string variable I'm trying to fill. The issue comes when I try to call the method

readArray(orbScript1.actualReadIn);

There's no errors with this, but it isn't filling the value on orbScript1. I'm pretty new at this, so if I'm doing something that is bad form or isn't common practice let me know.

If you need clarifications I'll do what I can to provide it.

Thanks.

Ciwerk
  • 1
  • 1
  • Have you tried using [breakpoints](https://learn.microsoft.com/en-us/visualstudio/debugger/using-breakpoints?view=vs-2019) to figure out what's going on there? – Serg Apr 08 '20 at 22:03
  • I suggest reading about [passing values by reference versus passing by value](https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value). Your method `readArray` is trying to assign a value to `actualRead`, but this value will never leave the scope of the method. Meaning, when you call `readArray(orbScript1.actualReadIn);`, you aren't actually modifying the value inside orbScript1. Read about passing by value vs reference to learn why that is. – Sean Skelly Apr 08 '20 at 22:49
  • Note also that I don't recommend using the `ref` keyword to solve your problem; I think you would be better off passing the entire `orbScript1` object into a method, and then grabbing its `actualReadIn` property so you can both get it and set it in the same method. – Sean Skelly Apr 08 '20 at 22:52

1 Answers1

0

I think I've got it. As it turns out, readArray() is completely useless. The way I actually got an output was

if (orbScript1.actualReadIn == "")
{
orbScript1.actualReadIn = Categories[checkForNotNull (Categories)];
}

This methods doesn't ask for an argument that is outside of the script, and fills the actualReadIn variable on my object.

Ciwerk
  • 1
  • 1