- Home /
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 .
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
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
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);
}
}
}
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.
@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
![](https://koobas.hobune.stream/wayback/20220612180842im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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