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 Ful · Nov 29, 2012 at 09:31 PM · rigidbodynullreferenceexceptionarrays

Problem with array on gameobjects

Hello Everyone,

I'm having some trouble on getting my script working and some help would be really appreciated. I've created an array and placed some game objects (petals) in it. I'm trying to do that every time I press the right mouse button then, the code looks for a gameobject within the array and add a rigidbody to it which causes the object to fall.

The sound does work, so the error has to be related to `petals.GameObject.rigidbody = petCharge[charge];`

I'm familiar with using arrays to replace texture, but never tried it on gameobjects before. Thanks in advance!

The Error: Nullreferenceexception object reference not set to an instance of an object

The Code:

 // Custom
 
 static var charge : int = 0;
 var petalsound : AudioClip;
 
 // The Array
 
 var petCharge : GameObject[];
 var petals : GameObject;
 
 function Start(){
 
     charge = 0;
 }
 
 function Update(){
 
      //Custom
     if (Input.GetButton("Fire2")){
     
     SendMessage ("Petalchange");
     
     }
 }
 
 // Button
 
 function Petalchange () {
 
     if(charge >= petCharge.Length -1) // Setting the limit to the charge to be -1 as length
            return;
     
     audio.PlayOneShot(petalsound);
     charge++;
     petals.gameObject.rigidbody = petCharge[charge]; // I get the error here
     print ("works");
 }
Comment
Add comment · Show 2
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 dannyskim · Nov 29, 2012 at 10:09 PM 0
Share

petals.GameObject.rigidbody = petCharge[charge];

GameObject should be gameObject, in any case, petals is declared as a GameObject, calling gameObject on a GameObject is a redundant lookup.

avatar image Ful · Nov 29, 2012 at 10:21 PM 0
Share

I see, thanks for the info but unity is now telling me that GameObject.rigidbody is read only

2 Replies

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

Answer by Bunny83 · Nov 29, 2012 at 11:24 PM

I'm still not sure if i got it right. This is what i got: You have an array of gameobjects. Every time you press down the right mouse button you want one / the next object in this array to drop (attach a rigidbody).

There are some things wrong:

  • GetButton will return true as long as you hold the button down. So you will call Petalchange every frame while the button is down. Use "GetButtonDown" instead.

  • You shouldn't use Sendmessage when you call another function in the same class. Just invoke the function like this: Petalchange();

  • You do a range check, but you check before you increment the variable. So when you reach the limit you can still get out of bounds. Do the check either after you changed the variable or change the variable after you used it.

  • Array indices start at "0" since you increment your variable before you use it you will start with "1" and effectively skip the first one.

  • Like DanielKim said the rigidbody property is read only. It can only return a value.

  • Also components can't be created and later be attached to a gameobject. Components can't exist without a GameObject they're attached to. You have to use AddComponent to attach a component to a gameobject.

  • Even when the assignment to the rigidbody property would work, you try to assign a GameObject to it. That doesn't make much sense.

Here's an example how to to what you've described:

 private var current : int = 0;
 var petalsound : AudioClip;
 var petCharge : GameObject[];
 
 function Update()
 {
     if (Input.GetButtonDown("Fire2"))
     {
         Petalchange();
     }
 }
 
 function Petalchange()
 {
     if(charge > petCharge.Length -1)
         return;
 
     audio.PlayOneShot(petalsound);
 
     petCharge[charge].AddComponent.<Rigidbody>();
     charge++;
 }

edit
If you want to drop all objects at once, just use a loop like this:

 // ...
 function Petalchange()
 {
     audio.PlayOneShot(petalsound);
     for (var i = 0; i < petCharge.Length; i++)
         petCharge[i].AddComponent.<Rigidbody>();
 }
Comment
Add comment · Show 2 · 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 Ful · Nov 29, 2012 at 11:43 PM 0
Share

Thank for the info, I'll be sure to be more careful in the future.

avatar image agentnikisanjes · Nov 30, 2012 at 04:24 PM 0
Share

yes unity very cool game engine !

avatar image
0

Answer by dannyskim · Nov 29, 2012 at 10:29 PM

Well then, rigidbody is read only, lol.

What you can do too is set your petal prefab to have a rigidbody by default, and set it to isKinematic by default, then activate it through code.

 petal.rigidbody.isKinematic = false;

Or, you need to AddComponent for the rigidbody, which would be:

http://docs.unity3d.com/Documentation/ScriptReference/GameObject.AddComponent.html

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 Ful · Nov 29, 2012 at 10:51 PM 0
Share

I see well this does work for a single petal, but I have a group of them within an array, how would I be able to trigger each of these petals?

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

13 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

Related Questions

NullReferenceException with a turret shooting a bullet 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

NullReference when accessing GameObject in array (C#) 1 Answer

how to handle multiple collider in this section? 0 Answers

Spaceship physics - adding resistance 2 Answers


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