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 /
avatar image
0
Question by unity_PkeTMQ-bDf-2XQ · Mar 06, 2020 at 07:57 AM · arraytriggerfollow player

I'm trying to make a conga line of objects. Am I using arrays wrong?

I'm trying to make a game where objects can be collected (via a trigger) and will join a line of identical objects to make a conga line.

Obviously each object should be following the one directly in front of it, so my idea was to use an array so it could identify which object within that array should be followed, based on how many are already following the player, which is why I'm using a static variable so they can all keep track of how many are currently following.

However, what I have now isn't working. It says 'Object reference not set to an instance of an object'. Am I using arrays wrong?

 using System.Collections;
 using UnityEngine;
 
 public class FollowerScript : MonoBehaviour
 {
     bool isActive = true;
     static int followerAmount = 0;
     public GameObject playerRabbit;
     GameObject followTarget;
     float DistanceFromTarget;
     Animator animator;
 
     void Start()
     {
         animator.GetComponent<Animator>();
     }
 
     void FixedUpdate()
     {
         //Rabbit will only follow if it has been activated.
         if (isActive)
         {
             FollowTheTarget();
         }
     }
     
     //When the player moves close to the follower it will activate.
     private void OnTriggerEnter(Collider other)
     {
         //Array contains all the followers and the player character.
         GameObject[] Rabbits = GameObject.FindGameObjectsWithTag("Rabbit");    
         playerRabbit = Rabbits[0];
         // If the collided object is the player and the follower has not already been activated.
         if (other == playerRabbit && !isActive)
         {
             //Default follower amount is zero, the player.
             followTarget = Rabbits[followerAmount];
             isActive = true;
             followerAmount++;
         }
     }
 
     void FollowTheTarget()
     {
         //Makes the followers stop a certain distance from the target.
         DistanceFromTarget = Vector3.Distance(transform.position, followTarget.transform.position);
         if (DistanceFromTarget > 2)
         {
             //Moves the follower towards the target + animations.
             transform.position = Vector3.MoveTowards(transform.position, followTarget.transform.position, 0.3f);
             animator.SetBool("IsRunning", true);
             transform.LookAt(followTarget.transform);
         }
         else
         {
             animator.SetBool("IsRunning", false);
         }
     }
 }
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
0

Answer by tormentoarmagedoom · Mar 06, 2020 at 10:23 AM

Hello.

First, for all Null reference error, we post this meesage :


Good day. This is a "standard response" For this kind of problems, you need to learn to find your problem by your own. As you can imagine, we can not try to read all scripts people posts, understand the logic and process, what all variables means, when or how you use them, and find where is the "problem". You are the only one who can do it... You need to debug the code while running, and check the states of all variables at the moment you see the problem, and I'm sure you will detect what is not how it was suposed to be. If don't know how, look for some tutorials on how to debug code while running. Bye & good Luck!


Learn about Null reference, is the most common error. It means some variable is null when code tries to read it.

If you dont say at what lineyou have the rror, is 100% impossible to guide you, but look, i say guide, not solve the problem. The problem can be obnly solved by you, we can not test the code, only you can.

Learn to debug and detect what variable is still null and then investigate why.

good luck!

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

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

160 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 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 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

Finding What Material Is On The Object Player Collided With. 1 Answer

trigger audio and text from array 0 Answers

Triggering a specific sound in an Array - syntax help likely... 1 Answer

Make an array/list of objects in enemy sight trigger 1 Answer

Arduino LDR Sensor to Play Audio Clip At A Certain Level 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