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 GunLengend · Sep 25, 2014 at 01:13 PM · valuemodifyanother script

Modify a value from another script ( no static value )

It take me over 2 days searched Google and have see more similar but still i don't get any working code.

I have 2 objects called Dog and Vase, with each object have their script. When Hero OnEnterCollision2D with Vase, the Vase broken and the Dog move to the Vase. The problem is the dog only move 1 time and my solution is modify a bool value ( this value must not be static because i must using this.). Here is the Trap.cs

     void Update () {
             if (hit && !breaked) 
             {
             
             if(this.tag == "chuoi")
             {
                 //Dont care about here, just something....
             }
             else if(this.tag == "xo")
             {
                 Debug.Log("Ban vua tam mien phi");
                 anim.SetBool("hit",true);
                 hit = false;
                 //Here it not working, i wanna modify wakeUp in Dog.cs
                 Dog dog = gameObject.GetComponent<Dog> ();
                 dog.wakeUp = true;
 
                 breaked = true;
                 posBreak = transform.position;
 
             }
 
 And the `Dog.cs`
 public class Dog : MonoBehaviour {
         public bool hitWall;
         public bool wakeUp;
 
     void Update () {
         //Debug.Log ("hitWall " + hitWall);
         //Debug.Log ("onWakeUp " + onWakeUp);
         if (wakeUp && !hitWall) 
         {
             Debug.Log ("Ban vua danh thuc con cho");
             this.transform.position = Vector2.MoveTowards (transform.position, TrapManager.posBreak, speed * Time.deltaTime);
             if(this.hitWall )
             {
                 this.transform.position = Vector2.MoveTowards (transform.position, transform.position, 0);
             }
         } 
 
     }
 
     void OnCollisionEnter2D(Collision2D col)
     {
         if (col.gameObject.tag == "wall") {
                         //Here that why i wanna modify wakeUp value, i just only wanna stop move the dogs which was collision with walls, not all the dogs, so this.wakeUp is required
             this.hitWall = true;
             this.wakeUp = false;
             Debug.Log("Cho vua dung tuong");
         }


When run, i got "Object reference not set to an instance object". Sorry for stupid question, i'm new in Unity, and search not solve my problem, so please help. Thank you.

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 GunLengend · Sep 29, 2014 at 03:02 AM 0
Share

Object reference not set to an instance object

1 Reply

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

Answer by $$anonymous$$ · Sep 25, 2014 at 04:23 PM

You can only use this.GetComponent<SomeType>() if component SomeType is attached onto current gameobject, otherwise it will return NULL. if you want to access Dog.cs from Trap.cs you can create: public GameObject DogObject; in trap.cs, assign Dog game object to this variable through editor and then you can use: Dog DogEntity = this.GetComponent(); in Trap.cs

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 GunLengend · Sep 29, 2014 at 03:05 AM 0
Share

Aha, i missing "assign Dog game object to this variable through editor" so it always said "Object reference not set to an instance object" and return null. I think you answer solve problem i got :D Thank you ZyTr0n, and thank you Harshdad$$anonymous$$ for the comment.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

transform.position related compile error fixed don't understand why ? 1 Answer

Modify circular progress bar based on a value? 1 Answer

Unity error, can't modify transform.position value 1 Answer

Modifying CharacterController center (C#) 1 Answer

Changing a dictionary value from another script 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