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 PJisAnarchist · Feb 16, 2012 at 03:41 PM · androidiosfindgameobjectswithtagrigid

GameObjects Array Problem

Hello everyone! First post here, but I already spent much time here looking for answers, and this community is awesome. I hope someday I'll be skilled enough to help others.

Here is what I'm trying to do: When I pick an item, it allows me to modify some rigidbodie's drag parameter, so it slows them if I press a key. It all works fine, except I got this error: NullReferenceException: Object reference not set to an instance of an object PlayerDefense.FixedUpdate () (at Assets/2DJumpNRunFramework/Scripts/PlayerDefense.js:57)

But, it works fine when I'm playing in Unity. The problem is when I play with my GS2 on Unity Remote, sometimes it works, sometimes nothing happens when I press the key. And when I compile for Android and test it directly on my GS2, nothin ever happens.

Here is the code for when I press the button:

function SetSlowTime(slowget:float){ slowtime=slowtime+slowget; }

function FixedUpdate () {

 if(FdefenseDown() && slowtime>0){
     print("baboum!");
     slowtime=slowtime-1;        
     var Slowable = GameObject.FindGameObjectsWithTag("slowable" || "bullet");
     if(Slowable!=null){
     for(var g : GameObject in Slowable) {
     g.GetComponent(SlowScript).slow();
         }
     }
 }                

}

And here is the code placed on every rigidbody:

 function slow () {
 
  if(rigidbody.drag == 0){
      rigidbody.drag = 20;
      rigidbody.angularDrag = 10;
      yield WaitForSeconds (5);
      rigidbody.drag = 0;
      rigidbody.angularDrag = 0;}
  else{
      rigidbody.drag = 0;
      rigidbody.angularDrag = 0;}
  }

Does anyone knows about an issue with modifying rigidbodies properties on mobile devices?..

Thanks in advance. Julien

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
0
Best Answer

Answer by save · Feb 16, 2012 at 04:12 PM

There are a couple of things I can see in your code that isn't very mobile friendly,

Use #pragma strict in the top of your script if you don't have it already.

You should connect deltaTime to any time parameter to be independent on all devices. Or you could make use of InvokeReapeating to save a few CPU-cycles.

Try to work around finding objects in realtime, instead do it at Start() or Awake() and fetch them into an array. Then you can use something like:

 private var slowArray : Rigidbody[] = new Rigidbody[10];
 for (var i : int = 0; i<slowArray.length; i++) {
     slowArray[i].drag = 20;
     slowArray[i].angularDrag = 10;
     //and so on
 }

For-loops are extremely fast!

Don't forget to reuse objects instead of creating new ones.

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 PJisAnarchist · Feb 16, 2012 at 07:17 PM

Thanks a lot for the advices, I'll apply that. About my problem itself, the error came from a misplaced tag on an object wich didn't have the "slow" script.

About the bug, it came from the "else" part of my slow script. By getting 2 items, I allowed 2 access to the script. So the first time it would slow the rigidbodies, then on the second press inside the 5sec timelapse, it would deactivate it. It worked well on computer, but on mobile device, the touch system often get 2 or 3 taps instead of 1. That's why the slow order got cancelled as soon as it was launched. I added a delay between the taps orders and everything is fine now.

Anyway, thanks a lot Save, for your precious advices. It will help the game going faster, but have a lot of scripts to rewrite that way now! ^^

Julien

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

6 People are following this question.

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

Related Questions

Android/IOS First Person Shooter 2 Answers

Divide resolution and stretch to screen/window/canvas 1 Answer

Audio Clip won't play on iPad when loaded from Resources folder 0 Answers

Store/Stack Items 0 Answers

Unity 5 splash screen problem? (Android and iOS) 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