Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Change value of script component via other script

Discussion in 'Scripting' started by SamuraiKitty, Nov 2, 2015.

  1. SamuraiKitty

    SamuraiKitty

    Joined:
    Jun 29, 2015
    Posts:
    16
    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<SurfaceCreator>();
    ...
    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
     
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    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.
     
  3. SamuraiKitty

    SamuraiKitty

    Joined:
    Jun 29, 2015
    Posts:
    16
    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.
     
  4. descenderdante

    descenderdante

    Joined:
    Sep 3, 2009
    Posts:
    218
    Could you maybe share a bit more of the script .. I don't see anything wrong with the way your changing the value
     
  5. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    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.
     
  6. SamuraiKitty

    SamuraiKitty

    Joined:
    Jun 29, 2015
    Posts:
    16
    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):
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEditor;
    4.  
    5. public class CamDistance : MonoBehaviour {
    6.  
    7.     float distance;
    8.     public GameObject camera;
    9.  
    10.     private SurfaceCreator sc;
    11.     private SpherifyTest st;
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.         sc = GetComponent<SurfaceCreator>();
    16.         st = new SpherifyTest();
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.         distance = Vector3.Distance(this.transform.position, camera.transform.position);
    22.         print(distance);
    23.  
    24.         GetComponent<SurfaceCreator>().resolution = 100 - (int)distance;
    25.        
    26.         st.Refresh();
    27.     }
    28. }
    29.  
    Then the code to create and set the resolution of the plane, SurfaceCreator:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
    5. public class SurfaceCreator : MonoBehaviour {
    6.  
    7.     private Mesh mesh;
    8.  
    9.     [Range(1, 200)]
    10.     public int resolution = 10; //The int I'm trying to change at runtime with the CamDistance script
    11.  
    12.     private int currentResolution;
    13.  
    14.    // SpherifyTest st;
    15.  
    16.     private void OnEnable () {
    17.         if (mesh == null) {
    18.             mesh = new Mesh();
    19.             mesh.name = "Surface Mesh";
    20.             GetComponent<MeshFilter>().mesh = mesh;
    21.         }
    22.         Refresh();
    23.     }
    24.  
    25.     public void Refresh()
    26.     {
    27.         if (resolution != currentResolution)
    28.         {
    29.             CreateGrid();
    30.         }
    31.     }
    32.  
    33.     private void CreateGrid()
    34.     {
    35.         currentResolution = resolution;
    36.         mesh.Clear();
    37.  
    38.         Vector3[] vertices = new Vector3[(resolution + 1) * (resolution + 1)];
    39.         Color[] colors = new Color[vertices.Length];
    40.         Vector3[] normals = new Vector3[vertices.Length];
    41.         Vector2[] uv = new Vector2[vertices.Length];
    42.         float stepSize = 1f / resolution;
    43.         for (int v = 0, y = 0; y <= resolution; y++)
    44.         {
    45.             for (int x = 0; x <= resolution; x++, v++)
    46.             {
    47.                 vertices[v] = new Vector3(x * stepSize - 0.5f, y * stepSize - 0.5f);
    48.                 colors[v] = Color.black;
    49.                 normals[v] = Vector3.back;
    50.                 uv[v] = new Vector2(x * stepSize, y * stepSize);
    51.             }
    52.         }
    53.  
    54.         mesh.vertices = vertices;
    55.         mesh.colors = colors;
    56.         /*mesh.triangles = new int[] {
    57.             0, 2, 1,
    58.             1, 2, 3
    59.         };*/
    60.  
    61.         int[] triangles = new int[resolution * resolution * 6];
    62.         for (int t = 0, v = 0, y = 0; y < resolution; y++, v++)
    63.         {
    64.             for (int x = 0; x < resolution; x++, v++, t += 6)
    65.             {
    66.                 triangles[t] = v;
    67.                 triangles[t + 1] = v + resolution + 1;
    68.                 triangles[t + 2] = v + 1;
    69.                 triangles[t + 3] = v + 1;
    70.                 triangles[t + 4] = v + resolution + 1;
    71.                 triangles[t + 5] = v + resolution + 2;
    72.             }
    73.         }
    74.  
    75.         mesh.triangles = triangles;
    76.         mesh.normals = normals;
    77.         mesh.uv = uv;
    78.     }
    79. }
     
  7. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    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.
     
  8. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    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?
     
  9. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    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.
     
  10. SamuraiKitty

    SamuraiKitty

    Joined:
    Jun 29, 2015
    Posts:
    16
    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
    Code (CSharp):
    1. st.Refresh();
    to
    Code (CSharp):
    1. sc.Refresh();
    2. st.Refresh();
     
  11. SamuraiKitty

    SamuraiKitty

    Joined:
    Jun 29, 2015
    Posts:
    16