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 /
  • Help Room /
avatar image
0
Question by AcidMage · Feb 06, 2016 at 06:12 PM · c#floatarray of gameobjectsgameobject.find

Help with arrays and checking other object's floats

Hey all,

So here's a quick run down of what I'm trying to achieve: the player clicks on a number of objects, which creates an "order" for which their character moves between them (moves to the first object picked, then the second, then the third, etc). Each object that can be clicked on has a "Target Script" attached to them (as well as any other script they need). This all works fine and dandy at the moment, as the objects are able to recognise when they've been targeted/when the player character has reached them, etc. The only problem is getting the player to move between objects. Initially, I had this handled by having separate "node" objects instantiate, which the player would then look for, but this seemed a bit much to me.

So what I want to have happen is the following: -When the player hits "go", their character finds all the targeted objects in the level (They bear the tag "target" and their bool "targeted" is set to true). -The character sees what order these objects have been clicked on in (handled with the NodeTargetNo float). -Using their own "NodesHit" float, the character moves from node to node ("I've hit 2 targets/nodes, so now I go to the 3rd).

With my current method, the character is only targeting the first, and it seems to be by sheer luck he is actually targeting the first anyway. Can anyone shed some light on a smoother way to find all the targets, while ordering them based on their own float? Should I be storing the targets earlier on, or using arrays or something similar to cache them and order them there?

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovementScript : MonoBehaviour
 {
 
     //Setting up most of the variables
     public float speed = 5f;
     public float NodesHit = 0f;
     public float NodesPlaced = 0f;
     public bool InAction = false;
     public TargetScript TargetScript;
     
     void Awake ()
     {
 
     }
 
     void Update ()
     {
 
         if (InAction == false) {
             Debug.Log ("Planning!");
         }
 
         //The state of the player when he is "In Action"
         if (InAction == true) {
             if (NodesHit != NodesPlaced) {
                 GameObject Target = GameObject.FindGameObjectWithTag ("Target");
                 TargetScript = Target.GetComponent<TargetScript> ();
                 if (TargetScript.NodeTarget == true) {
                     if (TargetScript.NodeTargetNo == NodesHit + 1) {
                         transform.position = Vector2.Lerp (transform.position, Target.transform.position, speed * Time.deltaTime);
                     }
                 }
             }
             if (NodesHit == NodesPlaced) {
                 return;
             }    
         }
     }
 
     //Check for collision with targets and progress to the next one
     void OnTriggerEnter2D (Collider2D other)
     {
         if (InAction == true) {
             if (other.gameObject.tag == ("Target")) {
                 TargetScript = other.GetComponent<TargetScript> ();
                 if (TargetScript.NodeTarget == true) {
                     if (TargetScript.NodeTargetNo == NodesHit + 1) {
                         TargetScript.Triggered = true;
                         NodesHit += 1f;
                     } 
                     else {
                         return;
                     }
 
                 }
             }
         }
     }
     
 }

Thanks for your help!

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

0 Replies

· Add your reply
  • Sort: 

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

How can I check the color of GameObjects in an Array and print out their color? 0 Answers

Timer float not evaluating correctly when I check if it's less than another number 3 Answers

error CS0266 Cannot implicitly convert type `float' to `int'. An explicit conversion exists (are you missing a cast?) 2 Answers

error CS1525: Unexpected symbol `float' 1 Answer

float value is reset after every update 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