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 UndyingSpite · Nov 04, 2013 at 09:11 PM · objectsinteract

Interact with Objects with scripts

Hi,

i want to open a door and i have 3 scripts.


First Script (Select) Assigned to GameCamera:

void Update () {

     Ray ray = transform.camera.ScreenPointToRay(new Vector3(Screen.width/2, Screen.height/2,0));

     if ( Physics.Raycast(ray, out hit,4) && hit.collider.gameObject.GetComponent<Interact>() != null)
     {
         hit.collider.gameObject.GetComponent<Interact>().OnLookEnter();    
     }
 }


Second Script (Interact) Assigned to any Object i want to interact with (in this case the door:

void Update () {
canInteract = false; renderer.material.shader = origShader; }

 public void OnLookEnter()
 {        
     canInteract = true;
     renderer.material.shader = Shader.Find("Self-Illumin/Bumped Diffuse");
     
 }


Third Script (OpenDoor) Assigned to the Door to open

void Update () {

     canInteract = this.GetComponent<Interact>().canInteract;
     
     if (open == true)
     {
         var target = Quaternion.Euler(0,DoorOpenAngle,0);
         door.localRotation = Quaternion.Slerp(door.localRotation,target,Time.deltaTime * smooth);        
     }
 
     if (open == false)
     {
         var target1 = Quaternion.Euler(0,DoorCloseAngle,0);
         door.localRotation = Quaternion.Slerp(door.localRotation,target1,Time.deltaTime * smooth);        
     }
 
     if (canInteract)
     {
     
         if (Input.GetMouseButtonDown(0))
         {
             if (open == false)
             {        
                 (open) = true;
                 
             }
             else
             {
                 (open) = false;
                 
                 
             }
             
             this.PlayOpenDoorSound();
         }        
     }    
 }

so if i walk to the door, the "canInteract" from the Interact script changes right, but the door "canInteract" which references to the "canInteract" from the Interact-Sript in the openDoor script does not Change. What did i wrong ?

Thx!

Comment
Add comment · Show 1
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 3lem3n7 · Nov 04, 2013 at 09:23 PM 0
Share

From what I see, you set your canInteract variable back to false in the Update function. So it might be that it gets reset back to false immediately after being set to true.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by UndyingSpite · Nov 04, 2013 at 09:57 PM

that's right, but i have to, cause when i leave the object (door) and onLookEnter is not called, it has to be false, imo. it works well with the shader, when i focuse the door it flashes and when i leave it gets the Default shader back.

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
Wiki

Answer by Starwalker · Nov 04, 2013 at 10:12 PM

What you are trying is already answered here:

link text

You can talk to other scripts primarily by: A: Making a gameobject in Script A, and placing the Object you want to interact with in it, you can get the Children by GetComponentInChildren.

B: Use the above method, which you already are, but I dont know if canInteract is a public field, hence you might need to declare it a "static" so that its visible outside the instantiation of an object. If you dont get the value via a "public" field like canInteract, try 'public static bool canInteract'.

You cant access a field "canInteract" without the object being already created, that too via a Updating function. Update function gets called after Awake and Start, which would mean that you are trying to access a field without its object actually existing which would lead to a null error.

Hope that helps.

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 UndyingSpite · Nov 04, 2013 at 11:16 PM 0
Share

I dont't get it. I try the example in the link (static variable) i set the field to static but has no effect.

The field "canInteract" in the script "Interact" changes when i leave/pass the door.

But "canInteract" in the script "OpenDoor" will not effected.

avatar image UndyingSpite · Nov 04, 2013 at 11:19 PM 0
Share

when i disable the "canInteract = false" in the Interact script, it works, but only once. of course it will never Change back to false.

avatar image Starwalker · Nov 05, 2013 at 12:48 AM 0
Share

In your Update () , you make the canInteract = false once every frame, hence when your OnLookEnter makes it true, its true for ONLY those frames...

if this is a time line, it will look like this:

FFFFFFFFTTTTTTTTFFFFFFFFFFFFFFF ... Frames

The T, or True states only happens for a few frames, and it sets it back to default, False, since Update() changes it back to false.

$$anonymous$$eaning, you should not update the bool inside an update function or change your monitoring mechanism.

In order to solve your problem, make the bool true via a On$$anonymous$$ouseOver function, not an update, and move your canInteract on this function. On$$anonymous$$ouseOver is only called if the object gets $$anonymous$$ouseOver'ed. Now once your interaction with the door is done, set this value back to false, else you wont be able to open it again as default.

you can use this: canInteract = !canInteract on LateUpdate. LateUpdate gets called once after Update()

On series of events: Awake() -> Start() -> Update() [every frame] -> LateUpdate() [Everyframe + 1], FixedFrame() [for Physics]

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

17 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 avatar image avatar image avatar image avatar image

Related Questions

Accessing variables from another object? 1 Answer

Scripts on imported objects do not work? 1 Answer

Can I Access Objects or Scripts in an Additive Level? 2 Answers

Converting object into script 1 Answer

Accessing other objects 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