Change value of script component via other script

I have 48 game objects with the same script on them, but I only want to change one of the public values of one of those objects, which is an int. I want to change the int of the script attached to a certain object. I don’t get any errors but it just doesn’t work.

The script is called SurfaceCreator, and here is my code to change the int of the gameobject’s component.

private SurfaceCreator sc = GetComponent();

sc.resolution = 100 - (int)distance;
//the int I’m trying to change is called resolution

If I click on the gameobject in the inspector while at runtime, I can see the value has changed but the changes don’t take effect.

Thanks

If you can see your value changing there is probably something wrong with how it is used. You need to show this. Please use code tags.

I don’t think there’s something wrong with how it’s used… If I manually change the value in the inspector (at runtime) the changes take effect, but they don’t take effect when changed in a script.

Could you maybe share a bit more of the script … I don’t see anything wrong with the way your changing the value

You are sharing very little information. When you show the script, we can get a better idea of what is going on and could e.g. definitely exclude that the script is the cause for the issue. If you don’t share the information, we need to guess.

Sorry… Here are the full scripts that are involved in this question:

Basically I’m trying to get a cube with 16 generated planes on each side, then to implement LOD, I would set the resolution based on camera distance. The cube with 16 planes on each side get’s combined and spherified, then when I need to change the resolution of one of the planes, I set the resolution value with the script that changes the value in the script component itself, clear the combined mesh, then combine and spherify again. This is done with the Refresh() method.

CamDistance.cs (Script that gets camera distance and tries to set value at runtime):

using UnityEngine;
using System.Collections;
using UnityEditor;

public class CamDistance : MonoBehaviour {

    float distance;
    public GameObject camera;

    private SurfaceCreator sc;
    private SpherifyTest st;

    // Use this for initialization
    void Start () {
        sc = GetComponent<SurfaceCreator>();
        st = new SpherifyTest();
    }
   
    // Update is called once per frame
    void Update () {
        distance = Vector3.Distance(this.transform.position, camera.transform.position);
        print(distance);

        GetComponent<SurfaceCreator>().resolution = 100 - (int)distance;
       
        st.Refresh();
    }
}

Then the code to create and set the resolution of the plane, SurfaceCreator:

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class SurfaceCreator : MonoBehaviour {

    private Mesh mesh;

    [Range(1, 200)]
    public int resolution = 10; //The int I'm trying to change at runtime with the CamDistance script

    private int currentResolution;

   // SpherifyTest st;

    private void OnEnable () {
        if (mesh == null) {
            mesh = new Mesh();
            mesh.name = "Surface Mesh";
            GetComponent<MeshFilter>().mesh = mesh;
        }
        Refresh();
    }

    public void Refresh()
    {
        if (resolution != currentResolution)
        {
            CreateGrid();
        }
    }

    private void CreateGrid()
    {
        currentResolution = resolution;
        mesh.Clear();

        Vector3[] vertices = new Vector3[(resolution + 1) * (resolution + 1)];
        Color[] colors = new Color[vertices.Length];
        Vector3[] normals = new Vector3[vertices.Length];
        Vector2[] uv = new Vector2[vertices.Length];
        float stepSize = 1f / resolution;
        for (int v = 0, y = 0; y <= resolution; y++)
        {
            for (int x = 0; x <= resolution; x++, v++)
            {
                vertices[v] = new Vector3(x * stepSize - 0.5f, y * stepSize - 0.5f);
                colors[v] = Color.black;
                normals[v] = Vector3.back;
                uv[v] = new Vector2(x * stepSize, y * stepSize);
            }
        }

        mesh.vertices = vertices;
        mesh.colors = colors;
        /*mesh.triangles = new int[] {
            0, 2, 1,
            1, 2, 3
        };*/

        int[] triangles = new int[resolution * resolution * 6];
        for (int t = 0, v = 0, y = 0; y < resolution; y++, v++)
        {
            for (int x = 0; x < resolution; x++, v++, t += 6)
            {
                triangles[t] = v;
                triangles[t + 1] = v + resolution + 1;
                triangles[t + 2] = v + 1;
                triangles[t + 3] = v + 1;
                triangles[t + 4] = v + resolution + 1;
                triangles[t + 5] = v + resolution + 2;
            }
        }

        mesh.triangles = triangles;
        mesh.normals = normals;
        mesh.uv = uv;
    }
}

In the CamDistance script, you are changing the resolution for the surface creator, but you forget to refresh the surface creator. You only refresh the spherify test.

What is the SpherifyTest object?
That is the only thing you call Refresh on during Update in CamDistance, are you sure you’re not meaning to call Refresh on the SurfaceCreator?

Side note;
You’re storing a reference to the SurfaceCreator as the variable sc, but you still use GetComponent in Update instead of using the sc reference.

Wow… I can’t believe how tiny things like that just get past you and you don’t notice… it works!
But for some reason, refresh is only called on SurfaceCreator.cs, not SpherifyTest. The generated plane is changing resolution like it should be, but it isn’t being spherified, which only means that Refresh() in SpherifyTest isn’t called. It’s weird because I have a print() set up in the Refresh() method, and it prints, meaning it is getting called, but it’s not combining the new mesh and spherifying. I can post the SpherifyTest.cs script here if you want to take a look at it to see the problem.

EDIT: Forgot to mention that I changed the line

st.Refresh();

to

sc.Refresh();
st.Refresh();

I got it working! Thanks heaps @ThermalFusion and @Dantus