Going to put this here too in case answer comes from here =)
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.
Now I know there is some serious issues with my logic here, but please, help me to make this work, I am sure it would help me later =)
First you need to retrieve all the clones of CubePrefab:
var allComponents = (CubePrefab[]) FindObjectsOfType(typeof(CubePrefab));
Then you can work through the list and check the number in each prefab:
foreach(CubePrefab prefab in allComponents)
Debug.Log(prefab.try1);
Sorry, but I suck with JS =(
Is there any way you could translate this to C#. And does this code actually give me start/updated values, or only 0 to everything?
That is C#.
It gives you the current values at the time you loop through the components.
Oh, sorry, always though var = JS…
Well still, this code wont work, it says I type or namespace name could not be found unity
just to clarify, my object Prefabs name which has the script in it is named Terrain1Prefab and scripts name is number1 as I said before…
and in case it matter, the Script where all this is going is inside prefab PelaajaPrefab.
And yes, I dragged Terrain1Prefab inside the inspector of Pelaajaprefab in the HereComesPrefab slot.
‘var’ is supported as of C# 3.0 (IIRC). It’s just a shorthand that saves you from having to write the type out, it means “figure the type out for me.”
Can you paste the exact compile error you’re seeing?