#Object Reference Issue

1 messages · Page 1 of 1 (latest)

feral ingot
#

Now it is just saying type mismatch when I dragged the item onto it

#

Object Reference Issue

urban girder
#

what item?....and please at least rename the instance name.....
now i see the storeXXX method, it is an instance method not static method

feral ingot
#

I renamed it to a lowercase p already

#

the item is PowerUpSpawner

urban girder
#

you cant drag this game Object to the field of PowerUpTouch.cs?

feral ingot
#

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.

urban girder
#

can you show the console message or the warning message when you try to drop the component to the field?

feral ingot
#

the other one when I go to drag is just a no symbol

#

like this

#

but in black and white

urban girder
#

it may because prefabs are trying to reference a on scene gameObject

#

i am not sure

feral ingot
#

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

urban girder
#

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

feral ingot
#

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

urban girder
#

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;
  }
}
feral ingot
feral ingot
urban girder
#

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

feral ingot
#

right so that would be a script called like enemy.cs or something

urban girder
#
GameObject newEnemy=Instantiate(enemyPrefab);
newEnemy.GetComponent<EnemyScript>().Hp=some Random Number;
feral ingot
#

and then to reference it, I need to know about Enemy to get -> hp

#

so does GetComponent add it?

urban girder
#

no, it return the component which already attached to the GameObject, if no then return null

feral ingot
#

so how are we attaching that script to the newEnemy

urban girder
#

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......

feral ingot
#

so you are referencing a variable in another script?

urban girder
#

yes

#

by getcomponent

feral ingot
#

ah okay thank you

#

will try to mess around with it