C
C#2y ago
benny9952

✅ enemy player follow

when i make a clone of enemy it uses original enemy position. i need a work around. either a way to get a clones position or someone can tell me im dumb and show me a better way to do this thank
66 Replies
TheRanger
TheRanger2y ago
CloneEnemy.transform.position = new Vector2(10, 10);
benny9952
benny9952OP2y ago
Thanks I can’t test this now but won’t this just change the position of the spawned clone or am I wrong?
TheRanger
TheRanger2y ago
yes, isnt that what you want? if you just want the position just call it
benny9952
benny9952OP2y ago
No the problem lies with the way I make the enemy follow the player it uses the original enemy’s position not the clones position do you know how I could call the clones position instead
TheRanger
TheRanger2y ago
CloneEnemy.transform.position thats how u call it
benny9952
benny9952OP2y ago
Thanks keep getting this error msg any idea on how to fix UnassignedReferenceException: The variable CloneEnemy of EnemyAttack has not been assigned. You probably need to assign the CloneEnemy variable of the EnemyAttack script in the inspector. EnemyAttack.Update () (at Assets/Scripts/EnemyAttack.cs:22)
TheRanger
TheRanger2y ago
can u show code of that line?
benny9952
benny9952OP2y ago
Where gameobject.find(“enemy”)is switched with CloneEnemy
benny9952
benny9952OP2y ago
Vector3 AttackDir = (GameObject.Find("Player").transform.position - CloneEnemy.transform.position);
benny9952
benny9952OP2y ago
TheRanger
TheRanger2y ago
where is CloneEnemy defined? in waves class?
benny9952
benny9952OP2y ago
no
TheRanger
TheRanger2y ago
where then?
benny9952
benny9952OP2y ago
yes sorry wavews
TheRanger
TheRanger2y ago
well the error message tells you how to fix awesome
benny9952
benny9952OP2y ago
ok one sec thanks sorry im confused isnt each instance of CloneEnemy enemy? oh ok think i got it maybe please can you spell it out for me im dumb
TheRanger
TheRanger2y ago
CloneEnemy has no value assigned to it it doesn't know which Clone Enemy to refer to
benny9952
benny9952OP2y ago
oh ok thanks life saver
TheRanger
TheRanger2y ago
u have to find a reference of the enemy and assign it to the variable example CloneEnemy = GameObject.Find("Enemy");
benny9952
benny9952OP2y ago
i know its a big ask but could i stream in a vc 2 u cause im really not getting it every time i think i am im just dumb
TheRanger
TheRanger2y ago
very hard but u can try sending screenshots atleast
benny9952
benny9952OP2y ago
ok thanks
benny9952
benny9952OP2y ago
benny9952
benny9952OP2y ago
TheRanger
TheRanger2y ago
is EnemyAttack script attached to a gameobject?
benny9952
benny9952OP2y ago
TheRanger
TheRanger2y ago
ok so u have 2 Start methods i think the one in waves isnt being called because the one in EnemyAttack is being called instead since EnemyAttack inherits from waves not 100% sure u have to use Debug.Log for that
benny9952
benny9952OP2y ago
oh i was just checking location
benny9952
benny9952OP2y ago
is this better?
TheRanger
TheRanger2y ago
also yeah but you never assigned a value to the field cloneEnemy you declared a local variable of the same name and assigned a value to it instead
TheRanger
TheRanger2y ago
benny9952
benny9952OP2y ago
like this?
TheRanger
TheRanger2y ago
that affects performance Unity will always try to find that game object and assign it to the variable every frame you only need to assign it once
benny9952
benny9952OP2y ago
benny9952
benny9952OP2y ago
still getting same error msg and the prefabs wont spawn in
TheRanger
TheRanger2y ago
use Debug.Log to make sure cloneEnemy isnt null
benny9952
benny9952OP2y ago
ok
TheRanger
TheRanger2y ago
well if the prefabs dont spawn
benny9952
benny9952OP2y ago
it says its null
TheRanger
TheRanger2y ago
TheRanger
TheRanger2y ago
ur trying to Instantiate/Clone a null object
benny9952
benny9952OP2y ago
still says null but the prefabs spawn in and the original follows player
TheRanger
TheRanger2y ago
so the prefab got spawned? where does it say its null? and the 4 game objects that got spawned? what are their names show screenshot of ur unity where they got spawned
benny9952
benny9952OP2y ago
null UnityEngine.Debug:Log (object) EnemyAttack:Update () (at Assets/Scripts/EnemyAttack.cs:24) they are called enemy(clone)
TheRanger
TheRanger2y ago
all of them have the same name?
benny9952
benny9952OP2y ago
yeah
TheRanger
TheRanger2y ago
not like Enemy(Clone)(1) Enemy(Clone)(2) Enemy(Clone)(3) Enemy(Clone)(4) ?
benny9952
benny9952OP2y ago
all different names now i removed wrong thing
TheRanger
TheRanger2y ago
no wonder this returned null
benny9952
benny9952OP2y ago
still getting that
TheRanger
TheRanger2y ago
do something like GameObject.Find("Enemy(Clone)"); or u can just assign the variable in the CreateEnemy method
benny9952
benny9952OP2y ago
TheRanger
TheRanger2y ago
do GameObject.Find("cloneEnemy1"); then
benny9952
benny9952OP2y ago
yeah that one prefab now follows me how could i change "cloneEnemy" to get whaterver name it needs?
TheRanger
TheRanger2y ago
access the .Name Property of the GameObject and change it
benny9952
benny9952OP2y ago
like this?
TheRanger
TheRanger2y ago
ur trying to change their names right? u do cloneEnemy.name = "whateveryouwanttonameit";
benny9952
benny9952OP2y ago
im trying to make it so whatever name it is gets the location of that clone so trying to set cloneEnemy to any name of clone if that makes sense
TheRanger
TheRanger2y ago
GameObject.Find("cloneEnemy3").transform.position to get position of cloneEnemy3 im not sure what that means
benny9952
benny9952OP2y ago
like that instance so it can just look at itslef and be like im at this positon so like when you have a an object and it knows its that object so when it calls a method and the method know that its that one
TheRanger
TheRanger2y ago
look at itself? i dont really understand what ur saying
benny9952
benny9952OP2y ago
dont worry man im gonna assume there isnt a way to do what i want to do thanks for all the help
TheRanger
TheRanger2y ago
if english isnt ur first language can u try using google translate atleast?
titan 1-1
titan 1-12y ago
what is going on in here💀
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
benny9952
benny9952OP2y ago
@TheRanger don't know if you remember this but easiest fix ever Vector3 AttackDir = (GameObject.Find("Player").transform.position - transform.position); used trandform.position which will get the position of whatever it is talking about dont need a use object name
Want results from more Discord servers?
Add your server