Behavior values incorrectly set

Why is this not setting the int value variables correctly for each set of 9 sprites should be set 1,2,3, instead they are being set to 0,1,2,3? - I intend to set them all from 1 to 9 once I get this work right. Inspector didn’t work for “value” variable and now it is so can preset the first one to 1 but still it is setting the wrong values to the instances.

using UnityEngine;
public class GridManager2 : MonoBehaviour

{
// The GameObject to instantiate.
public GameObject G1;
public float variance;
public float xdispl;
public float xspace;
public float ydispl;
public float yspace;
//static Sprite[ ] SA;
//private int SAN;
void Start()
{
SpawnEntities();
}

void SpawnEntities()

{
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
//xdispl -300
//xspace 30
//ydispl 150
//yspace 30
Vector3 position;
position = new Vector3((-1*variance + xdispl + (i * xspace)), (variance + ydispl - (j * yspace)), 0);
GameObject a=Instantiate(G1,position, Quaternion.identity);
a.GetComponent().value = 1;
position = new Vector3(0 + xdispl + (i * xspace), variance + ydispl - (j * yspace), 0);

Instantiate(G1, position, Quaternion.identity);

position = new Vector3(((variance + xdispl + (i * xspace))), (variance + ydispl - (j * yspace)), 0);
a=Instantiate(G1, position, Quaternion.identity);
a.GetComponent().value = 2;
position = new Vector3((-1variance + xdispl + (i * xspace)), (0 + ydispl - (j * yspace)), 0);
a=Instantiate(G1, position, Quaternion.identity);
a.GetComponent().value = 3;
position = new Vector3(0 + xdispl + (i * xspace), 0 + ydispl - (j * yspace), 0);
Instantiate(G1, position, Quaternion.identity);
position = new Vector3((variance + xdispl + (i * xspace)), (0 + ydispl - (j * yspace)), 0);
Instantiate(G1, position, Quaternion.identity);
position = new Vector3((-1
variance + xdispl + (i * xspace)), (-1*variance + ydispl - (j * yspace)), 0);
Instantiate(G1, position, Quaternion.identity);

position = new Vector3((0 + xdispl + (i * xspace)), (-1variance + ydispl - (j * yspace)), 0);
Instantiate(G1, position, Quaternion.identity);
position = new Vector3((variance + xdispl + (i * xspace)), (-1
variance + ydispl - (j * yspace)), 0);
Instantiate(G1, position, Quaternion.identity);

}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Tileimage : MonoBehaviour
{

public int value;
public Sprite G1;
public Sprite G2;
public Sprite G3;
public Sprite G4;
public Sprite G5;
public Sprite G6;
public Sprite G7;
public Sprite G8;
public Sprite G9;
// Start is called before the first frame update
void Start()
{
SelectImage();
}
void SelectImage()
{
if (value == 1)
{
this.gameObject.GetComponent().sprite = G1;
}
else if (value == 2)
{
this.gameObject.GetComponent().sprite = G2;

}
else if (value == 3)
{
this.gameObject.GetComponent().sprite = G3;

}
else if (value == 4)
{
this.gameObject.GetComponent().sprite = G4;

}
else if (value == 5)
{
this.gameObject.GetComponent().sprite = G5;

}
else if (value == 6)
{
this.gameObject.GetComponent().sprite = G6;

Please use code tags this is not easy to read at all. Also, post the rest of the code since it’s cut off in what I think might be quite an important part.

But of course it’s 0-9, you don’t initialise your ‘value’ variable so it will default to 0.

!(http://)
Nothing important at the bottom of that cut off code. I set the value variable in the behavior so it should override default values.
Won’t accept my image links

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Tileimage : MonoBehaviour
{

    public int value;
    public Sprite G1;
    public Sprite G2;
    public Sprite G3;
    public Sprite G4;
    public Sprite G5;
    public Sprite G6;
    public Sprite G7;
    public Sprite G8;
    public Sprite G9;
    // Start is called before the first frame update
    void Start()
    {
        SelectImage();
    }
        void SelectImage()
        {
        if (value == 1)
        {
            this.gameObject.GetComponent<SpriteRenderer>().sprite = G1;
        }
        else if (value == 2)
        {
            this.gameObject.GetComponent<SpriteRenderer>().sprite = G2;

        }
        else if (value == 3)
        {
            this.gameObject.GetComponent<SpriteRenderer>().sprite = G3;


        }
        else if (value == 4)
        {
            this.gameObject.GetComponent<SpriteRenderer>().sprite = G4;

        }
        else if (value == 5)
        {
            this.gameObject.GetComponent<SpriteRenderer>().sprite = G5;

        }
        else if (value == 6)
        {
            this.gameObject.GetComponent<SpriteRenderer>().sprite = G6;


        }
        else if (value == 7)
        {
            this.gameObject.GetComponent<SpriteRenderer>().sprite = G7;

        }
        else if (value == 8)
        {
            this.gameObject.GetComponent<SpriteRenderer>().sprite = G8;

        }
        else if (value == 9)
        {
            this.gameObject.GetComponent<SpriteRenderer>().sprite = G9;

        }

    }


    }
using UnityEngine;
public class GridManager2 : MonoBehaviour

{
    // The GameObject to instantiate.
    public GameObject G1;
    public float variance;
    public float xdispl;
    public float xspace;
    public float ydispl;
    public float yspace;
    //static Sprite[] SA;
    //private int SAN;
    void Start()
    {
        SpawnEntities();
    }

    void SpawnEntities()
   
    {
        for (int i = 0; i < 9; i++)
        {
            for (int j = 0; j < 9; j++)
            {
               //xdispl -300
               //xspace 30
               //ydispl 150
               //yspace 30
                Vector3 position;
                position = new Vector3((-1*variance + xdispl + (i * xspace)), (variance + ydispl - (j * yspace)), 0);
                GameObject a=Instantiate(G1,position, Quaternion.identity);
                a.GetComponent<Tileimage>().value = 1;
                position = new Vector3(0 + xdispl + (i * xspace), variance + ydispl - (j * yspace), 0);
            
                Instantiate(G1, position, Quaternion.identity);

                position = new Vector3(((variance + xdispl + (i * xspace))), (variance + ydispl - (j * yspace)), 0);
                a=Instantiate(G1, position, Quaternion.identity);
                a.GetComponent<Tileimage>().value = 2;
                position = new Vector3((-1*variance + xdispl + (i * xspace)), (0 + ydispl - (j * yspace)), 0);
                a=Instantiate(G1, position, Quaternion.identity);
                a.GetComponent<Tileimage>().value = 3;
                position = new Vector3(0 + xdispl + (i * xspace), 0 + ydispl - (j * yspace), 0);
                Instantiate(G1, position, Quaternion.identity);
                position = new Vector3((variance + xdispl + (i * xspace)), (0 + ydispl - (j * yspace)), 0);
                Instantiate(G1, position, Quaternion.identity);
                position = new Vector3((-1*variance + xdispl + (i * xspace)), (-1*variance + ydispl - (j * yspace)), 0);
                Instantiate(G1, position, Quaternion.identity);
                position = new Vector3((0 + xdispl + (i * xspace)), (-1*variance + ydispl - (j * yspace)), 0);
                Instantiate(G1, position, Quaternion.identity);
                position = new Vector3((variance + xdispl + (i * xspace)), (-1*variance + ydispl - (j * yspace)), 0);
                Instantiate(G1, position, Quaternion.identity);

          
            }
        }
    }
}

The solution is to set ‘value’ to 1 when you create it.

I set in Inspector to 1 but it has stopped being visible again- in which line on which behavior should I set it when created? I get the nonreadable folder bug a lot- I have to keep setting Unity Hub as open as administrator.

public class Tileimage : MonoBehaviour
{
    public int value = 1;
...

As for the other issue, that has been covered in other threads and wouldn’t belong in the Scripting forum area.

value is a name reserved for C# syntax, change it to something else.

I changed it all but it made no difference it is still 1,1,2,3 - (I fixed using Inspector once before)

using UnityEngine;
public class GridManager2 : MonoBehaviour

{
    // The GameObject to instantiate.
    public GameObject G1;
    public float variance;
    public float xdispl;
    public float xspace;
    public float ydispl;
    public float yspace;
    //static Sprite[] SA;
    //private int SAN;
    void Start()
    {
        SpawnEntities();
    }

    void SpawnEntities()
      
    {
        for (int i = 0; i < 9; i++)
        {
            for (int j = 0; j < 9; j++)
            {
               //xdispl -300
               //xspace 30
               //ydispl 150
               //yspace 30
                Vector3 position;
                position = new Vector3((-1*variance + xdispl + (i * xspace)), (variance + ydispl - (j * yspace)), 0);
                GameObject a=Instantiate(G1,position, Quaternion.identity);
                a.GetComponent<Tileimage>().num = 1;
                position = new Vector3(0 + xdispl + (i * xspace), variance + ydispl - (j * yspace), 0);
               
                Instantiate(G1, position, Quaternion.identity);

                position = new Vector3(((variance + xdispl + (i * xspace))), (variance + ydispl - (j * yspace)), 0);
                a=Instantiate(G1, position, Quaternion.identity);
                a.GetComponent<Tileimage>().num = 2;
                position = new Vector3((-1*variance + xdispl + (i * xspace)), (0 + ydispl - (j * yspace)), 0);
                a=Instantiate(G1, position, Quaternion.identity);
                a.GetComponent<Tileimage>().num = 3;
                position = new Vector3(0 + xdispl + (i * xspace), 0 + ydispl - (j * yspace), 0);
                Instantiate(G1, position, Quaternion.identity);
                position = new Vector3((variance + xdispl + (i * xspace)), (0 + ydispl - (j * yspace)), 0);
                Instantiate(G1, position, Quaternion.identity);
                position = new Vector3((-1*variance + xdispl + (i * xspace)), (-1*variance + ydispl - (j * yspace)), 0);
                Instantiate(G1, position, Quaternion.identity);
                position = new Vector3((0 + xdispl + (i * xspace)), (-1*variance + ydispl - (j * yspace)), 0);
                Instantiate(G1, position, Quaternion.identity);
                position = new Vector3((variance + xdispl + (i * xspace)), (-1*variance + ydispl - (j * yspace)), 0);
                Instantiate(G1, position, Quaternion.identity);

             
            }
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Tileimage : MonoBehaviour
{

    public int num=1;
    public Sprite G1;
    public Sprite G2;
    public Sprite G3;
    public Sprite G4;
    public Sprite G5;
    public Sprite G6;
    public Sprite G7;
    public Sprite G8;
    public Sprite G9;
    // Start is called before the first frame update
    void Start()
    {
        SelectImage();
    }
        void SelectImage()
        {
        if (num == 1)
        {
            this.gameObject.GetComponent<SpriteRenderer>().sprite = G1;
        }
        else if (num == 2)
        {
            this.gameObject.GetComponent<SpriteRenderer>().sprite = G2;

        }
        else if (num == 3)
        {
            this.gameObject.GetComponent<SpriteRenderer>().sprite = G3;


        }
        else if (num == 4)
        {
            this.gameObject.GetComponent<SpriteRenderer>().sprite = G4;

        }
        else if (num == 5)
        {
            this.gameObject.GetComponent<SpriteRenderer>().sprite = G5;

        }
        else if (num == 6)
        {
            this.gameObject.GetComponent<SpriteRenderer>().sprite = G6;


        }
        else if (num == 7)
        {
            this.gameObject.GetComponent<SpriteRenderer>().sprite = G7;

        }
        else if (num == 8)
        {
            this.gameObject.GetComponent<SpriteRenderer>().sprite = G8;

        }
        else if (num == 9)
        {
            this.gameObject.GetComponent<SpriteRenderer>().sprite = G9;

        }

    }


    }

You never set num to anything higher than 3 in your Instantiate loop there.

I know I didn’t- I want to get it work right before I add all those lines. It should still be 1,2,3 because that is order that the objects are substantiated in not 1,1,2,3.

Just for sanity sake before anything else, do yourself a favor and learn arrays. I’ll give this one as a freebie to shorten your “TileImage” class.

public class TileImage: MonoBehavior{
    public int num = 1;
    public Sprite[] sprites;

    void Start(){
        SelectImage();
    }
    void SelectImage(){
        this.gameObject.GetComponent<SpriteRenderer>().sprite = sprites[num-1];
    }
}

And just because you change “num”, or “value” in the previous post, does not mean TileImage will update the SpriteRenderer’s sprite with SelectImage. Start only runs once, and I’m fairly certain Start happens on instantiation, meaning your change is too late being after instantiation, or even if it does change it will not be consistent.

In order to make the change you want, you have to learn 2 other concepts, public functions and parameters.

Expose SelectImage as a public function, and provide a parameter to be used by whatever calls it.

public void SelectImage(int mynum){
    this.gameObject.GetComponent<SpriteRenderer>().sprite = sprites[mynum-1];
}

Now, SelectImage can be used as such

...
a.GetComponent<TileImage>().SelectImage(1);//input your desired index
...

This change will make Start in TileImage redundant, so remove it before trying the public function.

1 Like

You don’t though, look:

// 1:
position = new Vector3((-1*variance + xdispl + (i * xspace)), (variance + ydispl - (j * yspace)), 0);
GameObject a=Instantiate(G1,position, Quaternion.identity);
a.GetComponent<Tileimage>().num = 1;
// 1 again (the default value of num):     
position = new Vector3(0 + xdispl + (i * xspace), variance + ydispl - (j * yspace), 0);
Instantiate(G1, position, Quaternion.identity);
// 2:
position = new Vector3(((variance + xdispl + (i * xspace))), (variance + ydispl - (j * yspace)), 0);
a=Instantiate(G1, position, Quaternion.identity);
a.GetComponent<Tileimage>().num = 2;
// 3:
position = new Vector3((-1*variance + xdispl + (i * xspace)), (0 + ydispl - (j * yspace)), 0);
a=Instantiate(G1, position, Quaternion.identity);
a.GetComponent<Tileimage>().num = 3;
// 1 again (the default value of num):
position = new Vector3(0 + xdispl + (i * xspace), 0 + ydispl - (j * yspace), 0);
Instantiate(G1, position, Quaternion.identity);
position = new Vector3((variance + xdispl + (i * xsp

You should also follow the suggestions from @Laperen as they’re solid, but may be overwhelming as you’re still new.

1 Like

I saw some of those suggestions before- I just get something that worked first. With all the Inspector hassle, I didn’t want to change the variables. I just had an enormous hassle when trying to duplicate the behavior to create this new working one.

Are you saying these suggestions have been recommended multiple times to you, you choose not to try them out, and then ask why your stuff ain’t working the way you want?

What I’ve suggested isn’t even a workaround, they are the basics of basics. You have to learn them to even progress.

Not suggested before- I found it before also found scriptable objects . Simple direct is better to start because you can produce something rather than have broken hard to understand code.

The instant problems I get with changing code that was working as far as the game needs.