#Object Reference Issue
1 messages · Page 1 of 1 (latest)
Now it is just saying type mismatch when I dragged the item onto it
Object Reference Issue
what item?....and please at least rename the instance name.....
now i see the storeXXX method, it is an instance method not static method
you cant drag this game Object to the field of PowerUpTouch.cs?
right
when I click the little circle with a dot in the middle, its listed but if I select it, I get type mismatch. If I try to drag it, I simply cant.
can you show the console message or the warning message when you try to drop the component to the field?
the other one when I go to drag is just a no symbol
like this
but in black and white
it may because prefabs are trying to reference a on scene gameObject
i am not sure
you may try to unpack the prefabs first then assign it(not recommend) or using dependencies injection
https://unity.huh.how/programming/references/simple-dependency-injection
That doesnt make sense to me going to be honest
I dont understand that and have read it before
like I get that you would be telling the type and naming it into a var
but I dont understand how I would initalize it
Is the gameObjects which have the power up touch script on it are prefabs?
i see the prefabs name, seems that they has this script on them
ya they are
I stored the prefabs into a list which then I instantiate from at random...or will be. I have the destroy script and was trying to pass the powerup that spawned but that is where I got a hiccup
i have tested it and unity even doesnt allow me to drop the gameobject on scene to the field of prefab so idk how to reproduce the type mismatch message
btw
the PowerUpTouch component needs power up procedural but prefabs cannot reference it
so when some gameObject on scene spawn the prefabs, it has to set the field of the script on spawned prefabs.
public class A:Monobehaviour{//script on prefabs
public B b;
}
public class B:MonoBehaviour:{
public GameObject goThatHasA;
void SpawnA(){
GameObject go=Instantiate(goThatHasA);
go.GetComponent<A>().b=this;
}
}
if you click the circle next to the field where you normally drag and drop it, the GameObject will show up and you can double click it
This confused me even more lol
ok
now suppose you want to spawn some enemy on scene, and each enemy should have different HP
public class EnemyScript:Monebehaviour{
public float hp;
}```
so when you spawn it, you want to set their hp
right so that would be a script called like enemy.cs or something
GameObject newEnemy=Instantiate(enemyPrefab);
newEnemy.GetComponent<EnemyScript>().Hp=some Random Number;
and then to reference it, I need to know about Enemy to get -> hp
so does GetComponent add it?
no, it return the component which already attached to the GameObject, if no then return null
so how are we attaching that script to the newEnemy
back to the example, install of setting the Hp but it sets the variable b of component A
in your case, set the variable powerUpProcedural of component PowerUpTouch
the script should be already attached when you make the prefabs......
so you are referencing a variable in another script?