Can't externaly edit a float value

I am new to unity and I am trying to instantiate an item to thereafter change two variables inside it. I don’t get any compiler errors, but none of the varables changed.

heres the script i want to change the value X and Y in:
using UnityEngine;
using System.Collections;

public class Point : MonoBehaviour {
public GameObject grass;
public GameObject sand;
float WGenMain;
public float minidelay = 0.01f;
public float Type = 0;
public float X;
public float Y;
// Use this for initialization
void Start ()
{
StartCoroutine (Wait ());
}

IEnumerator Wait()
	{ 
		yield return new WaitForSeconds(1);
	WGenMain = Random.Range(0, 100);
    //This will be refined significantly

    if (WGenMain > 50)
    	{    
		Type = 1;
    	}

	if (WGenMain < 50)
    	{
		Type = 2;
		}
	if (Type == 1) 
		{
		Instantiate (grass, transform.position, Quaternion.identity);
		}
	if (Type == 2) 
		{
		Instantiate (sand, transform.position, Quaternion.identity);	
		}

}



// Update is called once per frame
void Update () {

}

}

here’s the script which tries to change those two variables:
using UnityEngine;
using System.Collections;

public class Xgen : MonoBehaviour {

public GameObject Grass;
public GameObject point;
float NameNumber = 1;

// Use this for initialization

void Start ()
{
	

	if (this.name == "X") 
	{
	} 

	else 
	{
		float name = float.Parse (this.name);
		for (int i = 0; i < GameObject.Find ("Source").GetComponent<Sourcegenerator> ().Cube_radius * 2; i++) {
			var Object = Instantiate (point, new Vector3 (transform.position.x, i * 12 - GameObject.Find ("Source").GetComponent<Sourcegenerator> ().Cube_radius * 12, transform.position.z), Quaternion.identity);
			Object.name = (this.name + "," + NameNumber);
			float X = GameObject.Find (this.name + "," + NameNumber).GetComponent<Point> ().X;
			float Y = GameObject.Find (this.name + "," + NameNumber).GetComponent<Point> ().Y;
			X = 69f;
			Y = NameNumber;
			NameNumber++;
		}
	}
}

void LateUpdate()
{
    Destroy(GameObject.Find("" + this.name));

}
// Update is called once per frame
void Update () {

}

}

I looked at all the other questiopns asked and none of them helped me. Thanks in advance.

I made a few changes to the Start() function in your Xgen class. Here’s the code (explanations below):

void Start ()
{
    if (this.name == "X") 
    {
    } 
    else 
    {
        float name = float.Parse (this.name);
		
		// this function is very CPU heavy, so it's really not a good idea to keep calling it - therefore we're going to store the value instead
		float sourceCubeRadius = GameObject.Find ("Source").GetComponent<Sourcegenerator> ().Cube_radius;
		
        for (int i = 0; i < sourceCubeRadius * 2; i++) {
            var Object = Instantiate (point, new Vector3 (transform.position.x, i * 12 - sourceCubeRadius * 12, transform.position.z), Quaternion.identity);
            Object.name = (this.name + "," + NameNumber);
			
			// you already have a reference to "Object" so there's no need to use GameObject.Find()
            Object.GetComponent<Point> ().X = 69f;
            Object.GetComponent<Point> ().Y = NameNumber;
			
            NameNumber++;
        }
    }
}

So, first of all, I made a variable float sourceCubeRadius as you were calling the same function 2 times for every single time that loop ran - and as I mentioned in the code comment, the GameObject.Find() function is very CPU heavy, so it’s really not a good idea to keep calling it - instead we’re going to store it’s return value in this variable. Just to note though, calling this function won’t break anything, I just thought I’d mention it since it’s really bad practise to use it a lot like you were.

Now on with the actual issue. You were using this in your code:

float X = GameObject.Find (this.name + "," + NameNumber).GetComponent<Point> ().X;
float Y = GameObject.Find (this.name + "," + NameNumber).GetComponent<Point> ().Y;
X = 69f;
Y = NameNumber;

The above code literally does nothing at all. You got the value of the X and Y from the Point class and stored them in local variables. You then modified the local variables. But then you never did anything with those variables and the garbage collector just deleted them. What you meant to do was modify the variables from the Point class directly, rather than modifying local variables. Also, there was no need to use GameObject.Find() as you already had a reference to the object.

Hope this helps :slight_smile: