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 Annig · May 01, 2015 at 04:08 AM · scripting problemtriggerenable

Enabling another script when entering a triggered plane (C#)

Hi all,

I've been working on a game where, as a first step, a group of objects start bouncing when they are approached by the player. So far I've solved this by creating a plane and placing it under the group of objects, and setting it as a trigger. Then, in my script PlayerControls, I've made an OnTriggerEnter function, where the Bouncing script is supposed to be enabled upon collision.

I did some searching, and wrote something that should work, but doesn't. Instead I get the error NullReferenceException: Object reference not set to an instance of an object on the line startBouncing.enabled = false; in PlayerControls. So apparently something goes wrong when referencing to the Bouncing script. However, I can't find what goes wrong here as I don't see the difference between my code and the ones referenced.

Note here that I tried disabling the Bouncing script as a first attempt instead of enabling it. Ideally, it would be disabled until I enabled it upon entering the trigger.

Here are the scripts I've got:

Bouncing

 using UnityEngine;
 using System.Collections;
 
 public class Bouncing: MonoBehaviour {
     public bool Move = true;
     public Vector3 MoveVector = Vector3.up;
     //public float MoveRange = 0.00001f;
     //public float MoveSpeed = 0.2f;
     private Bouncing bounceObject;
     Vector3 startPosition;
     
     void Start ()
     {
         bounceObject = this;
         startPosition = bounceObject.transform.position;
     }
     // Update is called once per frame
     void Update () 
     {
         if(Move) {
             bounceObject.transform.position = startPosition + MoveVector * (0.5f * Mathf.Sin(Time.timeSinceLevelLoad * 3.3f));
         }
     }
 }
 

PlayerControls

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class PlayerControls : MonoBehaviour {
     public float speed;
     private Rigidbody rb;
     private Bouncing startBouncing;
 
 
 
     void Start ()
     {
         rb = GetComponent<Rigidbody>();
         startBouncing = GetComponent<Bouncing>();
 
     }
 
     void FixedUpdate ()
     {
         float moveHorizontal = Input.GetAxis ("Horizontal");
         float moveVertical = Input.GetAxis ("Vertical");
 
         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
         rb.AddForce (movement * speed);
     }
     void OnTriggerEnter(Collider other) 
     {
         if (other.gameObject.CompareTag("Pick Up"))
         {
             other.gameObject.SetActive (false);
 
         }
         if (other.gameObject.CompareTag("Plane"))
         {
             startBouncing.enabled = false;
         }
     }
 
 }
 

Here's the main reference that I used: http://unity3d.com/learn/tutorials/modules/beginner/scripting/enabling-disabling-components

If anyone could help me out, I'd greatly appreciate it! I feel like I'm missing a tiny thing but I just can't find it.

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

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

Answer by siaran · May 01, 2015 at 08:51 AM

The error you get means that the startBouncing variable in your PlayerControls script is null. I see you have startBouncing = GetComponent(); in your Start() function, so that doesn't work properly. Keep in mind that GetComponent() only gets components that are on the same gameObject, not ones that are in a child object (or on another one entirely). Maybe you should use GetComponentInChildren() there instead?

Comment
Add comment · Show 4 · 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 Annig · May 04, 2015 at 10:19 AM 0
Share

Unfortunately, that does not seem to work, and results in the same error :( The Bouncing script is attached to a Prefab containing several bouncing cubes. I've checked whether assigning the script to a separate cube does the trick, but that doesn't solve it.

avatar image AeonIxion · May 04, 2015 at 11:04 AM 1
Share

It's always a good idea to check if your variable is null so your game won't crash.

if your startbouncing is null after a getcomponent is used, that means there is no bouncing script attached to the object that has the playercontrols script.

Bouncing script is attached to a seperate gameobject you say. So you should create a public gameobject variable and attach the gameobject with the bouncing script to that variable in the inspector. Then call getcomponent on that variable.

example:

 public Gameobject bouncingObject;
 private Bouncing startbouncing;
 
 void Start()
 {
      startbouncing = bouncingObject.GetComponent<Bouncing>();
 }
avatar image hbalint1 · May 04, 2015 at 11:12 AM 1
Share

I think the problem is as you said the bouncing class is on a plane. as @siaran mentjoned you can acces the components that are on the same GameObject with the GetComponent method. you should first search the object with the bouncing script on and after you can do GetComponent on it. So:

 startbouncing = GameObject.Find("Plane").GetComponent<Bouncing>();
avatar image Annig · May 04, 2015 at 11:27 PM 0
Share

Yes, using GameObject.Find worked! Thanks for the help!

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

Void OnTriggerEnter not working 1 Answer

Script has been enabled during the gameplay, but never called. 0 Answers

Don't react to colliders in children. 0 Answers

Can child game object with OnTriggerEnter function use parent game object's collider? 2 Answers

Turning script not enabling/disabling? 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