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 superventure · Mar 24, 2011 at 07:15 PM · mousecomponentitemsclosestequip

Refrencing components: What am I doing wrong here? (probably simple)

}if(pet2.length > 0 && pet2!=null) { var closestpet2 = pet2[0]; var dist = Vector3.Distance(transform.position, pet2[0].transform.position);

             for(var i=0; i < pet2.Length; i++) {            
             var tempDist = Vector3.Distance(transform.position, pet2[i].transform.position);            
             if(tempDist < dist) {                
             closestpet2 = pet2[i];            
         }}

         }if (closestpet2!=null && Vector3.Distance(closestpet2.transform.position, transform.position ) <=5 )
             if(held01)
             if(Input.GetMouseButtonUp(1)) 
             if (closestpet2.GetComponent(Follow).equip03== false){
         transform.parent = closestpet2.GetComponent("petmouth2").transform;
         transform.position = closestpet2.GetComponent("petmouth2").transform.position;              
         closestpet2.GetComponent(Follow).equip03=true;
         held01=false;
         held03 = true;

on the transform parent = etc & transform.positon = etc, the editor keeps giving me errors about how it's a null reference exception. I'm trying to equip items the player is holding to the 'closestpet2's mouth by mouseclick. Thanks for any help

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 Statement · Mar 24, 2011 at 07:22 PM

If you're getting null reference exceptions it means you don't have a component called "petmouth2" on closestpet2. Are you sure you have a script that is called exactly "petmouth2"? Are you sure you aren't looking for a child instead of a component? You can use Find to find a child if that's what you're looking for, since it makes no sense to get the transform of a component of an object because it would be the same transform as closestpet2.transform.

Comment
Add comment · Show 3 · 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 superventure · Mar 24, 2011 at 07:26 PM 0
Share

How would I write that? The pets mouth is an empty game object child positioned at the mouth of the character. THis is the script is on the item itself

avatar image superventure · Mar 24, 2011 at 07:35 PM 0
Share

transform.parent = transform.Find("closestpet2/petmouth2"); transform.position = transform.Find("closestpet2/petmouth2").position;

I tried this with the link but it's still not going- what am I missing?

avatar image Statement · Mar 24, 2011 at 08:50 PM 0
Share

try closestpet2.transform.Find("petmouth2"). Are you really sure this is where the program crashes?

avatar image
1

Answer by Paul 7 · Mar 24, 2011 at 07:31 PM

This is the case in C# and i dont see why it wouldnt be in Java but with the statement:

if(pet2.length > 0 && pet2!=null)

the if statement checks pet2.length > 0 first and if pet2 IS null then a null reference will get thrown.

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 superventure · Mar 24, 2011 at 07:34 PM 0
Share

For it's 'petmouth' gameobject? When I start the player atleast 2 pet2s are active. I am using Java

avatar image Statement · Mar 24, 2011 at 08:52 PM 0
Share

What he means is you're checking pet2 for null in wrong order. Paul means that you should write if (pet2 != null && pet2.length > 0) since otherwise you can get a null reference exception when doing pet2.length.

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

No one has followed this question yet.

Related Questions

2D Animation does not start 1 Answer

How to change value of var of a disabled component? 0 Answers

disable/enable script js 2 Answers

How to fix mouse Look X and Y sensitivity (built-in script) 3 Answers

Move an object towards closest enemy. 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