Hellou mates, It would seem my previous threads haven’t appeared here, or so I hope or this is like my milionth post about same problem.
The problem, I have script which I attached to terrains. It has public float corner1y corner1x etc etc.
First I wanted to use them as a public static, but it only saved the first objects variables, so I changed it to just public.
Now, the problem comes when I try to get the info.
In player Script, it gets the value, but it is 0 for everything, since I only set values to them in void Start() and in update().
How can I get the updated values in case , let’s say, I am moving the platform. Is this possible at all or do I have to just find another way? In inspector, every object has their own values in script section, so it has to be possible, right?
The code in Player:
public class Player : MonoBehaviour {
public Terrain2 terrain;
and later
if (terrain.coord1y == coord3y)
Sorry all the errors and typos, I am just so pissed off since I couldn’t find answer anywhere, and have now been stuck for like 3 days.
If Terrain2 is a class that you made, apply the System.Serializable attribute to the class definition. From your description I assume that this is a Serialization problem. Unity by default only serializes objects that inherit from UnityEngine.Object so if your Terrain2 class does not inherit(directly or indirectly) from UnityEngine.Object you need to apply the System.Serializable attribute to it to tell Unity to serialize it.
I don’t understand what value you’re actually trying to access. I also have no idea what you’re trying to achieve, so it’s really difficult to give useful help.
Going to just simplify my promlemo here, so stay with me.
I create Script lets say:
public class number1 : MonoBehaviour {
public int try1;
void Update ()
{
try1 = Random.Range(0,12);
}
}
Now I make this component of CubePrefab, which has lets say 3 clones.
In another objects Script, I want to know all of these 3 numbers which I get, how do I do that?
I tried public number1 HereComesPrefab;
and after that I ask Debug.log(number1.try1);
but it only gives me 0 every time.
After writing this I realized there are some serious issues with my logic here, but please, help me to make this work, I am sure it would help me later =)
Thanks Bladefist, this might actually work, gonna try it out. Just have to find way to make it simple enough if I got lets say, try1-try8, so that they are all in one simple int[ ] numbers.
Make sure that you are not calling Debug.Log from Start(). With the code you have shown, the numbers will not be initialized by then(and will therefore be 0).
Ok, just a thought. When you reference the class, make sure to change the reference from the prefab to an actual in-game object. If you happen to be referencing a prefab, nothing will happen.
//make sure that this is not the component on the prefab
public number1 MyNumber;
void Update()
{
Debug.Log(MyNumber.try1);
}
for(int i = 0; i < gos.count; i++)
{
stuffwewant = gos*.gameObject.GetComponent().try1;* } (I wrote this code in my browser off my head so there may be some problems with it but i hope it helps or gives you an idea)
Not quite sure what you mean, I cant drag anything else there in Inspector, other than my prefab. And since every object in my Scene is “clone” from Prefab, how else can I do it?
public class example : MonoBehaviour {
void Awake() {
number1[] objectArray = FindObjectsOfType(typeof(number1)) as number1[];
foreach (number1 thisNumber in objectArray) {
Debug.Log(thisNumber.try1);
}
}
}
I wouldn’t use it intensively if I were you however. It comes with a steep performance cost. I’d cache all instances of number1 on a public array and reference that array instead.
If you have specific objects, but they are created at run time; Use a list object and gather them as you create them.
If you have a prebuilt specification, but do not want all of them, then you are going to have to make a List object and add them all to it, then go through the list on each one.
When you create the “clone” of the prefab just replace the reference with the clone. For example:
The script that is trying to access the ‘number1’ objects:
//this value is originally the prefab but, when we create a new object, it will be replaced.
public number1 number1Object;
//this is the prefab
public number1 number1Prefab;
//Creates a clone of the prefab
public void CreateNumber1Object()
{
// after we create the new object we want to replace our current value(number1Object)
// with the new value, so that we are not trying to get data from the prefab.
//create the object
GameObject createdObject = (GameObject)Instantiate(number1Prefab, Vector3.zero, Quaternion.identity);
//update the reference to our number1Object so that it does not reference the prefab anymore
number1Object = createdObject.GetComponent<number1>();
}
void Update()
{
//so when you access the object it is the updated one instead of the prefab
Debug.Log(number1Object.try1);
}
So that when you create a clone of the ‘number1’ prefab, we will just change the reference from the prefab to the clone.
Thank you everyone for the answers =P Sadly I could make none of them work. Diviners idea was good, but it still only gave me 0 from everything.
I am starting to believe that the reason these don’t work is because I just suck with unity and C# and so, I think I will just try to do everything from beginning again, and throw everything I have done so far to trash bin.
Thank you again for the help, and when I have learned some more, I will try to help others here too =)
Not sure if I understand you correctly, but you can instantiate a number of cubes from your GameManager and add each cube’s script component to a List<>.
Then in your GameManager simply loop through all the components in the list and get their value using a Get method…
i.e
CubeScript.cs (Attach this script to your cube prefab)
using UnityEngine;
using System.Collections;
public class CubeScript : MonoBehaviour {
int _try1;
void Update () {
_try1 = Random.Range(0, 12);
}
public int GetTryValue()
{
return _try1;
}
}
GameManager.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GameManager : MonoBehaviour {
[SerializeField] GameObject _cubePrefab; // Cube prefab
List<CubeScript> _cubes; // List to store all cube script components
int _numberOfCubesToInstantiate = 3;
void Start () {
_cubes = new List<CubeScript>();
for (int i = 0; i < _numberOfCubesToInstantiate; i++)
{
GameObject go = GameObject.Instantiate(_cubePrefab, Vector3.zero + new Vector3(i * 20, 5, 0), Quaternion.identity) as GameObject;
CubeScript cs = go.GetComponent<CubeScript>(); // Get the cubescript component of instantiated object
_cubes.Add(cs); // Add the component to your list
}
}
void Update ()
{
// Write to the log the values
foreach (CubeScript cs in _cubes)
Debug.Log(cs.GetTryValue().ToString());
}
}