Adding prefab objects to a list on another script and object?
Hi so I have a predicament, Im quite new to unity and coding in general, but I have been making a block stacking type game and so far I have been able to solve most problems.
But I have reached a point where I am completely stuck. So far I have blocks that spawn in at various spawn points above the camera and fall down with the option to move them left and right and then input ceases once they hit the floor or another block.
My issue is I wanted to have a height measuring system in place for a high score, the best way I could think of was to put all spawned objects into a list once they had hit another object. At the moment my BlockSpawner script sits on a game object in the scene and the blocks are prefabs that are not in the scene until the game runs.
Now that’s the bit Im stuck on I wanted the blocks to add themselves to the list in a different script that is attached to a different object and it has to work for all prefab clones that spawn, but I have no idea how!! I have looked at many videos and forum posts about using variables from other scripts but nothing seems to work so far.
Sorry for the long post any In detail explanation would be appreciated or even links to other posts that answer the question would be very much appreciated
Thank you
Answer by tormentoarmagedoom · May 13, 2018 at 10:23 PM
Good day.
There is no problem for that. First you need to know how to call a method from another script. Lets make an example:
A script called "BlocksController" is inside a GameObject called "GameController". This GameController have the tag assigned "GameCont". There is only one GameController and only one BlocksControler in the game, controlling this issue.
Now, all the blocks, have a script called "Block", are tagged with tag "Block", and have a Colldier and a RigidBody. We will make the block script (from each block object) tell to the BlocksControler script that he has touched another Block.
In the Block Script, you need to use OnCollisionEnter() to detect when the block has touched another collider, and will tell that if is another block, executes a method inside the BlockController script.
BlocksController Script:
List <GameObject> ListOfBlocks = new List <GameObject>;
public void AddBlockToList(GameObject BlockToAdd)
{
ListOfBlocks.Add(BlockToAdd);
}
So then i call this method, giving the block that has touched something as a parameter, it will be stored at the list.
Block script:
void OnCollisionEnter(Collision other)
{
if (other.tag == "Block")
{
GameObject.FindObjectWithTag("GameCont").GetComponent<BlocksController>().AddBlockToList(gameObject);
}
}
With this method, when the block touches another collider of an objecrt tagged with "Block", will execute the method inside the BlocksController giving itself as a parameter.
You - are- welcome :D
Bye!
PD: Accept the answer come on.. :D
Answer by TheMK-_- · May 14, 2018 at 10:58 PM
Fantastic just brilliant!!! Can't express how much this has helped THANK YOU =).
I did have to change if (other.tag == "Block")
to if (other.gameObject.tag == "Block")
because it was throwing up an error but no biggie.
Oh one problem I have noticed now that I have it working is the objects as they bounce slightly are adding themselves to the list multiple times.
Do you know how i might set it to trigger once or to stop them adding themselves multiple times?
But thanks again for your help really appreciate it.
Cheers :)
I actually managed to solve my own problem of the adding multiple times to the list with a Boolean where once the object has triggered onCollision the Boolean is set to true and then the code wont run after another collision.
But I now have another question
I wanted to find the object in the list with the Highest Y value and store it in a variable so that I could display it as the high score and use it to move the camera up. Do you know how I could do this? I already tried changing the list to a list of transforms rather than of game objects but I still don't quite know how to go about finding the highest Y Value?
Any help would be great
Thanks again.
Your answer
Follow this Question
Related Questions
UI Text Not Updating 1 Answer
Words in my script don't turn blue, why not? (absolute script noob) 3 Answers
Vector3 with two set axis but one free one 1 Answer
How to create a mesh at a location with code? 1 Answer
Disable script after time 1 Answer