- Home /
Can I check the name of an element?
Can I check the name of each element with other script?
For exampel check the element 6 from "Activator Script" with "Death Script"
Something like:
If element 6 name = "Death" Do something; If element 5 name = "Score" Do something;
the Gamearray variable seems to be an integer array. Integers don't have names. Can you explain what you're trying to do ?
I want to get a randomized effect for 10 object.
Null-Score-Death. As you can see in the animation that I created.
Umm... This is the script of Gamearray
function Start() { gamearray = new GameObject[10]; InvokeRepeating("ChangeArray", Random.Range(5, 15), Random.Range(5, 15)); for(var i : float = 0; i < 10; i++) { var chooseRandomly : boolean = (Random.value < 0.5); if(chooseRandomly == true) { gamearray[i] = score; } else { gamearray[i] = death; } } } function ChangeArray() { for(var i : float = 0; i < 10; i++) { var chooseRandomly : boolean = (Random.value < 0.5); if(chooseRandomly == true) { gamearray[i] = score; } else { gamearray[i] = death; } }
Answer by jdog98 · Jan 06, 2015 at 05:20 PM
You can acess one scripts variables with another script by using
gameObject.GetComponent("Activator").Gamearray[ ]
so somthing like...
if(gameObject.GetComponent("Activator").Gamearray[5] == "score"){
put that in the death script
I get this error"'Gamearray' is not a member of 'UnityEngine.Component'."
In C# it should be
gameObject.GetComponent<Activator>().Gamearray[5] == "score"
"The type or namespace name `Activator' could not be found. Are you missing a using directive or an assembly reference?"
Thats c#.... we are using java. Just stick with what I posted and check the spelling of Gamearray. Capitalization is important. You probably used a lowercase g ins$$anonymous$$d of G
Answer by InvincibleCat · Jan 07, 2015 at 05:55 PM
What you can do is have a static class or a Singleton that will handle your "Level". So he will store the number of death and score in the level (you can also have a DeathMax and ScoreMax values so you can adjust your design). Each time an element need to become : Null, Death or Score, it will ask the static class that will return what the element can be.
Here is the code of the manager. It replace both your Activator and your Checker scripts
using System.Collections.Generic;
using UnityEngine;
public class ActivatorManager : MonoBehaviour
{
private ActivatorManager _instance = null;
public ActivatorManager Instance
{
get { return _instance; }
}
public int NbScoreMax = 1;
public int NbDeathMax = 1;
public List<GameObject> Elements = null;
private int _nbScore = 0;
private int _nbDeath = 0;
private void Awake()
{
_instance = GetComponent<ActivatorManager>();
}
private void Start()
{
List<int> availableElements = new List<int>();
for (int i = 0; i < Elements.Count; i++)
{
availableElements.Add(i);
}
int index = Random.Range(0, availableElements.Count);
int elementIndex = availableElements[index];
availableElements.Remove(elementIndex);
ActivateDeath(Elements[elementIndex]);
index = Random.Range(0, availableElements.Count);
elementIndex = availableElements[index];
availableElements.Remove(elementIndex);
ActivateScore(Elements[elementIndex]);
for (int i = 0; i < Elements.Count; i++)
{
ActivateNull(Elements[availableElements[i]]);
}
}
public void ActivateDeath(GameObject pGameObject)
{
if (_nbDeath < NbDeathMax)
{
_nbDeath++;
DesactivateNull(pGameObject);
pGameObject.GetComponent<Death>().enabled = true;
}
else
{
ActivateNull(pGameObject);
}
}
public void ActivateScore(GameObject pGameObject)
{
if (_nbScore < NbScoreMax)
{
_nbScore++;
DesactivateNull(pGameObject);
pGameObject.GetComponent<Score>().enabled = true;
}
else
{
ActivateNull(pGameObject);
}
}
public void ActivateNull(GameObject pGameObject)
{
pGameObject.GetComponent<Null>().enabled = true;
}
public void DesactivateDeath(GameObject pGameObject)
{
pGameObject.GetComponent<Death>().enabled = false;
_nbDeath--;
ActivateNull(pGameObject);
}
public void DesactivateScore(GameObject pGameObject)
{
pGameObject.GetComponent<Score>().enabled = false;
_nbScore--;
ActivateNull(pGameObject);
}
public void DesactivateNull(GameObject pGameObject)
{
pGameObject.GetComponent<Null>().enabled = false;
}
}
Ok, let me remake the question, keep in $$anonymous$$d that I m newbie.
This is what i tried to do:
The ActivatorScript will randomize between Null-Death-Score
CheckerScript will check the elements and after check will activate a script (DeathScript, ScoreScript, NullScript) from a gameobject that corespond to his element (gameobject 1 - element 0).
But I saw that I can t check the element in this way.....
If you think that there is another way please let me know.
I want to somehow control the time when they appear/disapear and limit them to 2-3 score/death and rest of them to be null. Is that even possible?
Thank you all for your answers! I really appreciate this :)
I don't really understand what you are trying to do... What do you want to do? Not in technical terms but more in concept terms
The player has to pass from right to left and vice versa. This is why I try to make the objects appear randomly.
Umm... nope, but you can see in video what I want, except that there is no player to go from side to side.