What is... Implicit downcast from "unityEngine.Component" to "PickupController". ?

I get this warning about my PlayerController script at 16,57

I don’t know why it doesn’t like my PickupController script.
Everything was going fine as I was writing code. No errors. Then I went to Play/Check game and I notice that warning.

David

Instead of:

pickUpController = someComponent;

Try this:

pickUpController = (PickupController)someComponent;

If you’re using C# you could try adding as PickupController at the end (before the semi-colon)

// the PickupController component of the “PickupSpawnPoints” GameObject
private var pickupController:PickupController;

function Awake()
{
// retrieve the PickupSpawnPoints gameObject
var pickupSpawnPoints:GameObject = gameObject.Find(“PickupSpawnPoints”);

// and then retrieve the PickupController Component of the above PickupSpawnPoints gameObject
pickupController = pickupSpawnPoints.GetComponent(“PickupController”);
}

Don’t use quotes in GetComponent. That makes it cast to Component, rather than the correct type. Please use code tags when posting code.

–Eric

OK thank you for idea about Qoutes Eric5h5

I have tried to use tags before up here, but have never gotten it to work, even though I pressed “Go Advanced” for the entries.
How Exactly can I get these code tag things to work, Eric??

David Robinson

http://forum.unity3d.com/threads/143875-Using-code-tags-properly

–Eric

Well folks, the Warning went away, but so did my ability to pick up my items…oops… somewhere I am overlooking something. I have looked at this and rewritten it so many times, I am getting dizzy.

I have items I desire to randomly spawn. I have a timer on them, but think I will do away with random timer and just let them Spawn once.
I am going to put multiple items ( different items ) on the level and have them spawn once for pickup. But I am going to put Multiple Spawn positions for each item on each level. ( Probably 10 possible places for each individual item to show up on 1 Level )

I will send code immediately after this. Trying to get the Code Tag to work right now.

ok last try with Code Tags…

// PlayeController.js  script

#pragma strict

function Start () {

}

// the PickupController component of the "PickupSpawnPoints" GameObject
private var pickupController:PickupController;

function Awake()
{
      // retrieve the PickupSpawnPoints gameObject
      var pickupSpawnPoints:GameObject = gameObject.Find("PickupSpawnPoints");
      
      // and then retrieve the PickupController Component of the above PickupSpawnPoints gameObject
       pickupController = pickupSpawnPoints.GetComponent(PickupController);
}

function OnControllerColliderHit (hit:ControllerColliderHit)
{
   if (hit.gameObject.tag == "Pickup")
   {
         // call the Collected(....) function of the PickupController Component (script) and
         // pass the pickup we hit as the parameter for the function
         pickupController.Collected(hit.gameObject);
    }
}

----------------------------------------------------- NEXT CODE ----------------------------------------------------

// PickupController.js  script

#pragma strict

// minimum and maximum spawn delay time
public var minimumSpawnDelayTime:int = 1;
public var maximumSpawnDelayTime:int = 5;

// the pickup prefab, assigned via the Inspector
 public var pickupPrefab:GameObject;

// the spawnpoint that our pickup will be spawned at
  public var numberOfPickups:int = 3;
 
 // the ARRAY of spawnpoints that our pickup will be spawned at
 private var spawnPointList:GameObject[];
 
 // array of which spawn points are currently available for spawning at
 private var spawnIndexAvailableList:Array = [];
 
 // variable to hold the total number of spawn points, saves having to recalculate
 private var numberOfSpawnPoints:int;
 

function Awake()
{
  // retrieve Gameobjects tagged as "SpawnPoint" within the "PickupSpawnpoints" GameObject which this script is a Component of
	spawnPointList = gameObject.FindGameObjectsWithTag("SpawnPoint");

  // retrieve number of spawn points
	numberOfSpawnPoints = spawnPointList.length;

  // make sure number of pickups doesn't exceed mumber of spawn points
	if (numberOfPickups > numberOfSpawnPoints) numberOfPickups = numberOfSpawnPoints;

  // make all spawn points available by setting each index to true
	for (var i:int = 0; i < numberOfSpawnPoints; i++)
	 {
	    spawnIndexAvailableList[i] = true;
     }

   // spawn x amount of pickups according to numberOfPickups
   for (var j:int = 0; j < numberOfPickups; j++)
    {
      SpawnPickup();
    }
}

function SpawnPickup()
{
  // generate a random integer to use as the index to select a spawn point from the list
  var randomSpawnIndex:int = Random.Range(0, numberOfSpawnPoints);
  
  // while the selected spawn index is unavailable regenerate another one
  while (!spawnIndexAvailableList[randomSpawnIndex])
  {
     randomSpawnIndex = Random.Range(0, numberOfSpawnPoints);
  }

// retrieve the position and rotation of the pickups spawn point
  var spawnedPickupPosition:Vector3 = spawnPointList[randomSpawnIndex].transform.position;
  var spawnedPickupRotation:Quaternion = spawnPointList[randomSpawnIndex].transform.rotation;
  
// instantiate (create) the pickup prefab with the above position and rotation
  var spawnedPickup:GameObject = Instantiate(pickupPrefab, spawnedPickupPosition, spawnedPickupRotation);
  
// set the spawn pickup as a child of the "PickupSpawnPoints" gameobject that this script is a Component of
// This is so we can use SendMessageUpwards within scripts attached to the pickupPrefab to call functions within this script
  spawnedPickup.transform.parent = spawnPointList[randomSpawnIndex].transform ;
  
//set the name of the pickup as its index
  spawnedPickup.name = randomSpawnIndex.ToString();
  
// make the spawn index unavailable to prevent another pickup being spawned in this location
  spawnIndexAvailableList[randomSpawnIndex] = false;
} 
  
        
   function Collected(pickupCollected:GameObject)
  {
   // retrieve name of the collected pickup and cast to int
   var index:int = parseInt(pickupCollected.name);
   
   // pickup has been destroyed, so make the spawn index available again
   spawnIndexAvailableList[index] = true;
   
   // destroy the pickup
   Destroy(pickupCollected);
   
   // wait for a random amount of seconds between minimumSpawnDelayTime and maximumSpawnDelayTime
   yield WaitForSeconds(Random.Range(minimumSpawnDelayTime, maximumSpawnDelayTime));
   
   // spawn a new pickup
   SpawnPickup();
   }
   
function Start () {

}

function Update () {

}

Whew, Code Tag engaged this time. Kept SKIPPING the # sign selection…duh :smile:

Nobody’s going to look at that code unless it’s formatted properly using code tags. Please read the instructions in the link I posted; it’s very simple to do.

–Eric

have you double checked your pickups are actually tagged as “Pickup”? It’s surprisingly easy to create a tag but not assign it. (at first glance your code seems fine)

Yes, I just went back to Inspector to check the tag for the object. The object for pickup is called “pickup-model” and the tag is “Pickup”.

I had been picking up the model. Then I changed the code to re-instantiate the model after 1 - 5 seconds after it is picked up. If I put 20 pickup spots and only allowed 5 models to be seen, the model will show up again in one of the locations between 1 and 5 seconds after I pick it up.

I think I will remove the re-spawn command because my game froze up after I picked up the second model. Before I invoked the re-spawn command, I could pick up my model and keep looking for more.

Oh well, back to my original problem. I received that Warning and took away the QUOTATION marks from Line 18 in the PlayerController.js file at (PickupController).

I think this afternoon, I will reinstall the quotes and go back to my original warning. I need to make sure I did not change anything else in all my changes these past 3 days. If I pick up my model and get the warning, then I will continue to march.
Thank you folks for all the input.

OK, my initial problem of “downcast” has been resolved. I need to change the POST THREAD to another situation.
I lost my Pickup ability with all the changes I made to the code trying to resolve downcast problem.

Thank you folks for the input AND the help to finally get the Code Boxes to work up here.

Capital “GameObject” on line 15.