Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by TheMK-_- · May 13, 2018 at 05:54 PM · scripting problemscripting beginnerprefabslists

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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 :)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheMK-_- · May 15, 2018 at 03:57 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

209 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges