Controlling a GameObject from another GameObject's script component
Posted
by
OhMrBigshot
on Game Development
See other posts from Game Development
or by OhMrBigshot
Published on 2012-07-05T13:09:30Z
Indexed on
2012/07/05
15:24 UTC
Read the original article
Hit count: 311
I'm creating a game where when starting the game, a Cube is duplicated GridSize * GridSize
times when the game starts. Now, after the cubes are duplicated I want to attach a variable to them, say "Flag" which is a bool, from another script component (let's say I have a Prefab that generates the cloned cubes).
In short, I have something like this:
CreateTiles.cs : Attached to Prefab
void Start() {
createMyTiles(); // a function that clones the tiles
flagRandomTiles(); // a function that (what I'm trying to do) "Flags" 10 random cubes
}
CubeBehavior.cs : Attached to each Cube
public bool hasFlag;
// other stuff
Now, I want flagRandomTiles()
to set a Cube's hasFlag property via code, assuming I have access to them via a GameObject[]
array.
Here's what I've tried:
Cubes[x].hasFlag = true;
- No access.- Making a function such as
Cubes[x].setHasFlag(true)
- still no access. - Initializing Cubes as a CubeBehavior object array, then doing the above - GameObjects can't be converted to CubeBehaviors - I get this error when I try to assign the Cubes into the array.
How do I do this?
© Game Development or respective owner