Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by corporate_snack · Mar 15, 2013 at 08:23 AM · javascriptprefabtag

Script ignores prefabs/duplicates of generic object

I'm totally new to Unity, so this question might sound silly. I'm working on a custom "collision" system. When the player presses an arrow key, the code checks to make sure the "player" block is not right up against a "wall" block (both the "player" and "wall" blocks have dimensions 1x1x1), and if it is not, it moves 1 unit in the specified direction (x and z movement only).

I created a "player" cube and a "wall" cube in my scene, attached the script to the "player" cube, dragged the respective components from the hierarchy to the inspector, and tested it out. It worked great! However -- I then turned the "wall" cube into a prefab, and made another one, identical to the first, moved it by a few units so it wouldn't be in the same position, and tried again. My "player" cube moved right through this one, but still wouldn't move through the other one.

I can only assume the problem is that my code doesn't say anything about applying this script to multiple "walls", and thus will only work on one, even if they're exact duplicates of each other.

My question is: How can I get this script to apply to all 1x1x1 "wall" blocks I create, instead of working for one and totally ignoring all the others? Thanks in advance for any help! Sorry if this is too simple a question.

 var player : Transform;
 var wall : Transform;
 var speed : float = 5;
 
 while(true) yield CoUpdate();
  
 function CoUpdate() {
 
     if (Input.GetKey(KeyCode.RightArrow)
         && (player.position.x + 1 != wall.position.x
         || player.position.z != wall.position.z))
             yield Move(Vector3.right);
                     
     if (Input.GetKey(KeyCode.LeftArrow)
         && (player.position.x - 1 != wall.position.x
         || player.position.z != wall.position.z))
             yield Move(Vector3.left);
                 
     if (Input.GetKey(KeyCode.UpArrow)
         && (player.position.z + 1 != wall.position.z
         || player.position.x != wall.position.x))
             yield Move(Vector3.forward);
 
     if (Input.GetKey(KeyCode.DownArrow)
         && (player.position.z - 1 != wall.position.z
         || player.position.x != wall.position.x))
             yield Move(Vector3.back);
                 
     else 
         yield;
 }
  
 function Move(distance : Vector3) {
     var pt = player.transform;
     var goal = pt.position + distance;
     while (pt.position != goal) {
         pt.position = Vector3.MoveTowards(pt.position, goal, speed * Time.deltaTime);
         yield;
     }
 }
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

1 Reply

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

Answer by Tarlius · Mar 15, 2013 at 09:06 AM

You are only checking one of the objects (the object stored in wall). You probably want to use an array of some kind.

 GameObject[] walls; // Declaration that you're assigning in the editor

 // trying to move a direction
 foreach(GameObject wall in walls) {
     // Do stuff with "wall"
     if( CheckForCollisionHere(player, wall) ) {
         collisionFound = true;
         break;
     }
 }
 if( !collisionFound) DoMove();

You could set up the scene automatically, and there are also ways to use raycasting and colliders to check for collisions, but probably best to leave those for another day :)

On a side note, be careful using == with floats. Also worth mentioning is that "Generic" in a programming context usually means something different to your usage.

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

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

11 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

Related Questions

Identifying instances of prefabs in code 1 Answer

Prefab Cannot Select Object Correctly 0 Answers

My array does not update when object is destroyed. How do I fix it? (java) 2 Answers

How can I instantiate more prefabs as the score increases? 2 Answers

Change image visibility of newly instantiated prefab? 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