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 hunterjett2228 · Oct 23, 2018 at 09:38 PM · gameobjectcodepagepoints

I am having trouble assigning public gameObjects

I am Attempting to create a chain of points, that spawn in and out as the player moves about, so i cant just assign the public gameObjects directly. (I am making the points lights so i can see them better) but the points are tracking the distance of the "PLayerWithTrail"(the player) from the point. I am stuck and dont know how to assign "Target" to the movable player. this is the code that tracts the distance between two objects and i have the Origin (point) as a transform position and i need "Target" to be the player so i can destroy the point after it goes so far away.

using System.Collections; using System.Collections.Generic; using UnityEngine; public class Distance : MonoBehaviour { public GameObject Origin; public GameObject Target; void Start () { Origin = gameObject; Target = //here is where i am lost } // Update is called once per frame void Update () { distanceBetween = Vector3.Distance(Origin.transform.position, Target.transform.position); if (distanceBetween > 8) { // Target.AddComponent<Light>().enabled = false; Object.Destroy(gameObject); } } } and I have a separate code that spawns in the points in case it is useful:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PointInfo : MonoBehaviour
 {
     public Transform cubeyMcCuberstien;
     public float c;
     private float addidive = 5;
     private float countDuccu;
     private GameObject dotterDottyFace;
     private float curX, curY, curZ, detox;
     int num, death;  
     
     void Start ()
     {
         countDuccu = 5;
         c = countDuccu + addidive;
         death = num - 20;
     }
     
     void Update ()
     {
         curX = GetComponent<Transform>().position.x;
         curY = GetComponent<Transform>().position.y;
         curZ = GetComponent<Transform>().position.z;
             
         if (c <= GetComponent<Movement2>().moveCounter)
         {         
             dotterDottyFace = new GameObject("point"+num+"");
             //Components here
             dotterDottyFace.AddComponent<Distance>();        
            // dotterDottyFace.transform.parent = gameObject.transform;
             dotterDottyFace.transform.position = new Vector3 (curX, curY, curZ);
             dotterDottyFace.AddComponent<Light>();
             //dotterDottyFace.AddComponent<Distance>();
             //dotterDottyFace.AddComponent<DeathByTime>();
             c = GetComponent<Movement2>().moveCounter + addidive;
             num++;
             detox =death + 1 ;
         }
     }
 }
 Some of the things in this code are unnecessary but i just am trying to get a concept of what im doing before I go am optimize . if you have any suggestions as to how i can assign Target to PLayerWithTrail it would be greatly appreciated . 

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

3 Replies

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

Answer by CitizenVeen · Oct 24, 2018 at 12:32 PM

using

 target = GameObject.Find("Player");

will get you the player gameobject (if it is called "Player"). You are spawning and destroying the 'points' as you call them. I take it they are prefab then? you can also use:

 public gameObject target;

and on the prefab drag 'n drop the player into that slot in the inspector. If you don't know how to do that: you can see it in this vid @ 3:33: https://youtu.be/Q1xZGt41N80?list=PLFt_AvWsXl0fnA91TcmkRyhhixX9CO3Lw&t=212 futhermore you can use FindWithTag or even FindGameObjectsWithTag if you want to get an array of multiple objects. see the links for more info: https://docs.unity3d.com/ScriptReference/GameObject.Find.html https://docs.unity3d.com/ScriptReference/GameObject.FindWithTag.html https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html

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 hunterjett2228 · Oct 25, 2018 at 01:43 AM 0
Share

Your suggestion was the perfect fix. I thought i had tried GameObject.Find but I think I was spelling the name wrong or something. Thank you

avatar image
0

Answer by hunterjett2228 · Oct 23, 2018 at 09:41 PM

I am not sure why that first code got written like that here it is again: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class Distance : MonoBehaviour {
 
     public GameObject Origin;
     public GameObject Target;
 
     void Start ()
     {   
         Origin = gameObject;
        Target = //here is where i am confused        
     }
 
     void Update () {
        
         distanceBetween = Vector3.Distance(Origin.transform.position, Target.transform.position);
         if (distanceBetween > 8)
         {
             // Target.AddComponent<Light>().enabled = false;
             Object.Destroy(gameObject);            
         }        
     }
 }
 
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
avatar image
0

Answer by GamitusLabs · Oct 23, 2018 at 10:44 PM

Unless you are assigning it (drag and drop) in the inspector, you are going to have to give it a target from elsewhere in your program, the game manager or the target object.

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 hunterjett2228 · Oct 24, 2018 at 12:16 PM 0
Share

@GamitusLabs How do I give it a target from somewhere else? because that's what I'mm asking about. i need the "Target" to be assigned to the player from within the code. you mentioned the game manger, what is that and how could it possibly help me?

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

140 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

Related Questions

Need to increase the gameobject value . 2 Answers

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Ontrigger not working 0 Answers

score system for destroying objects 3 Answers

Pooling Issue 0 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