Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Oct 17, 2017 at 10:35 PM by $$anonymous$$ for the following reason:

Variables that were attempted to be assessed were missing in some objects,

avatar image
0
Question by $$anonymous$$ · Oct 17, 2017 at 02:05 AM · object referencescript reference

My script to make an object bob up and down as an effect is not working.

Hello, after being fed up that animating an object in the animation panel is not as versatile is doing an "animation" in code, I made a script that will make an object bob up and down. It worked fine until I attempted to reference another script's variable in order to tell it that my object must be matured first before it can bob up and down.

Error that I am getting (Only on the first frame when game starts):

NullReferenceException: Object reference not set to an instance of an object BobScript.Update () (at Assets/Scripts/BobScript.cs:26)

     private float Bob;
     private bool BobIncreasing;
     public float BobSize;
     public float BobSpeed;
     public GameObject BobbingObject;
     public string BobbingAxis;
     public bool CanBob;
     public TreeAI TScript;
     private bool Adult;
     public bool BobWhenAdult;
 
 
     void Start () {
         Bob = 0.8f;
     }
     
 
     void Update () {
         if (CanBob == true) {
             Adult = TScript.Adult;
             if (Adult == true && BobWhenAdult == true){
             if (Bob < 1 - BobSize/10) {
                 BobIncreasing = true;
                     }
                     if (Bob > 1) {
                         BobIncreasing = false;
                     }
                     if (BobIncreasing == true) {
                         Bob += 0.1f * BobSpeed * Time.deltaTime;
                     }
                     if (BobIncreasing == false){
                         Bob += -0.1f * BobSpeed * Time.deltaTime;
                     }
                     if (BobbingAxis == "X"){
                         BobbingObject.transform.localScale = new Vector3 (1 * Bob, 1, 1);
                     }
                     if (BobbingAxis == "Y"){
                         BobbingObject.transform.localScale = new Vector3 (1, 1 * Bob, 1);
                     }
                     if (BobbingAxis == "Z") {
                         BobbingObject.transform.localScale = new Vector3 (1, 1, 1 * Bob);
                 }
             }
         }
     }
 }
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

1 Reply

  • Sort: 
avatar image
1

Answer by StormMuller · Oct 17, 2017 at 08:59 PM

Your code is difficult to read. But have you tried:

var transform.position = new Vector3(transform.position.x, Mathf.Sin(Time.time), transform.position.z);

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 $$anonymous$$ · Oct 17, 2017 at 10:13 PM 0
Share

What is this line of code for? I am not trying to move my object anywhere, the problem is that something is not set properly, and so it is equal to null when access to it is attempted.

avatar image $$anonymous$$ · Oct 17, 2017 at 10:34 PM 0
Share

Turns out that I was being dumb and dragged the script on some other objects and forgot. The problem with that is that I was building the script towards one specific object and not the others, so when the code ran, it got confused that there was some things that were missing on the other objects.

Follow this Question

Answers Answers and Comments

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

Related Questions

OnNetworkLoadedLevel in the script reference? 0 Answers

Trying to assign new name, Unity says it can't find the object? 1 Answer

check collision of object B from object A 1 Answer

object reference is required to access non-static member 'GameManager.CompleteLever()' 1 Answer

hi,Why the raycast is having object reference error on the : "if (hit.collider.gameObject.tag == "Player") " line? thanks 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