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 Alienjesus · Mar 08, 2011 at 02:25 PM · updatebooleanpickup

Boolean being constantly redefined?

OK, the title here may be misleading as I'm not TOTALLY sure it's the issue, but it's the only thing i can think of right now. I'm writing a script that allows you to pick up and drop objects, only i cannot get the object to drop at all. Here's the if statement I currently have:

-function Update() {

var fwd = transform.TransformDirection (Vector3.forward); var hit : RaycastHit; var LayerMask = 1 << 9;

var otherThing: Transform; var hasObject = false;

Debug.DrawRay(transform.position, fwd, hit, 50, LayerMask);

if(Input.GetButton ("Fire2")){ if (hasObject){ otherThing.rigidbody.isKinematic = true; transform.DetachChildren(); //otherThing.parent = null; hasObject = false; print("Dropped "+otherThing.gameObject.name+"\n"); } else{ if (Physics.Raycast (transform.position, fwd, hit, 50, LayerMask)) { otherThing = hit.transform; otherThing.rigidbody.isKinematic = true; otherThing.parent = transform; hasObject = true; } else{ print ("Nothing to pickup!\n"); }

} } }

So my object gets picked up, but not put down again. The Object dropped message doesnt display either. I THINK the issue is that because i'm repeatedly defining hasObject as false due to it being in an Update function, but I don't know how to make it only define hasObject at false when the level loads whilst still being able to call it into my update function. Probably quite a rookie problem i admit :)

Thanks for the 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 burnumd · Mar 08, 2011 at 02:29 PM

If you're working in Javascript (which it appears you are), you simply move var hasObject = false somewhere in the same file outside the Update function (or any other function). If you're looking for a lengthy read on the concept at play here, Wikipedia has you covered.

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

Answer by Bob5602 · Mar 08, 2011 at 02:32 PM

Yep, that is exactly what you are doing :) Good catch. Just take all of your variable declarations out of the update function, cause you're re-creating them every frame. Below I did it for you just removing the hasObject boolean from the function. However, you should look through all of the variables you're setting up to make sure you couldn't save time just declaring them once then updating the value each frame.

var hasObject : boolean = false;

funciton Update(){

 var fwd = transform.TransformDirection (Vector3.forward);
 var hit : RaycastHit;
 var LayerMask = 1 &lt;&lt; 9;

 var otherThing: Transform;



 Debug.DrawRay(transform.position, fwd, hit, 50, LayerMask);

 if(Input.GetButton ("Fire2")){
    if (hasObject){
    otherThing.rigidbody.isKinematic = true;
    transform.DetachChildren();
    //otherThing.parent = null;
    hasObject = false;
    print("Dropped "+otherThing.gameObject.name+"\n");
      } else{
       if (Physics.Raycast (transform.position, fwd, hit, 50, LayerMask)) {
  otherThing = hit.transform;
  otherThing.rigidbody.isKinematic = true;
  otherThing.parent = transform;
  hasObject = true;
  } else{
 print ("Nothing to pickup!\n");
 }

 }
 }
 }

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 Alienjesus · Mar 09, 2011 at 10:13 AM 0
Share

Hey, after doing this I think it's almost working, but I still get an error message saying "NullReferenceException: Object reference not set to an instance of an object" referring to this line:

otherThing.rigidbody.is$$anonymous$$inematic = true;

Any ideas?

Don't worry if not, I'll keep working at it and post a seperate question later if I can't figure it out.

avatar image Bob5602 · Mar 09, 2011 at 02:32 PM 0
Share

Hmm. Your otherThing is a transform, so it won't have a rigidbody component. So you can't call otherThing.rigidbody because it doesn't exist, thats where your error is co$$anonymous$$g from. How to fix it is more tricky, since hit is a raycasthit which isn't a gameobject and has limited properties. In my experience with similar things, I would make otherThing a GameObject, and set it as the thing you hit using some find searching for the hit.name and matching them. Works if you have uniquely named objects, or you can rename it to something unique for the purposes of these actions. (more...)

avatar image Bob5602 · Mar 09, 2011 at 02:35 PM 0
Share

... However, it appears you are setting is$$anonymous$$inematic = true on both accounts. Is it ever false? if not, you don't really need those statements in there. Or, you could keep it as it is, and set the is$$anonymous$$inematic property on the object somewhere else. However, I have other problems with this script since you're not actually changing the object you hit at all, but are changing this 'otherThing' which is re-created every frame as a temporary transform. The script wouldn't do anything to any gameobject you run into.

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

How to make this code fire projectile once during the Update function? I know Update means every frame. 1 Answer

Making a level up button., 2 Answers

Since I can't add a yield during an update, how would I go around it? 1 Answer

GetComponent in the Update function? 1 Answer

Three Possible Conditions, Checked in Update, Print Condition Only On Condition Change 0 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